View Javadoc

1   /* (c) Copyright 2003 Caleigo AB, All rights reserved. 
2    * 
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2.1 of the License, or (at your option) any later version.
7    * 
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   * Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   *  
17   */
18  
19  package org.caleigo.core.service;
20  
21  
22  import java.io.*;
23  
24  import org.caleigo.core.*;
25  import org.caleigo.toolkit.log.*;
26  
27  /*** BinaryFileDataService provides a simple extension of the MemoryDataService
28   * that stores the data entities in binary files in database file-catalog.
29   *
30   * Note that the binary format used is the standard java serialization format
31   * that may vary between differnt versions of the JDK and CEL library. 
32   * The class should therfore only be used for tests, demonstrations or other
33   * temporary data storage solutions.
34   *
35   * @author  Dennis Zikovic
36   * @version 1.00
37   * 
38   *//* 
39   *
40   * WHEN        WHO               WHY & WHAT
41   * -----------------------------------------------------------------------------
42   * 2002-01-16  Dennis Zikovic    Creation
43   * 2003-01-02  Dennis Zikovic    Remade to use the MemoryDataService.
44   */
45  public class BinaryFileDataService extends MemoryDataService 
46  {
47      // Data members ------------------------------------------------------------
48      private File mDirectoryFile;
49      
50      // Constructors ------------------------------------------------------------
51      public BinaryFileDataService(IDataSourceDescriptor dataSourceDescriptor, String dbDirectoryPath) 
52      {
53          this(dataSourceDescriptor, dataSourceDescriptor.getSourceName(), dbDirectoryPath);
54      }
55      
56      public BinaryFileDataService(IDataSourceDescriptor dataSourceDescriptor, Object serviceIdentity, String dbDirectoryPath) 
57      {
58          super(dataSourceDescriptor, serviceIdentity);
59          
60          // Create root directory file.
61          try
62          {
63              mDirectoryFile = new File(dbDirectoryPath);
64              
65              // Try to create directory if it doesn't exist.
66              if(!mDirectoryFile.exists())
67                  mDirectoryFile.mkdir();
68              
69              // Check read/write access.
70              if(mDirectoryFile==null)
71                  Log.printError(this, "Failed to initialize FileDataService, path not found \""+dbDirectoryPath+"\"!");
72              else if(!mDirectoryFile.canRead() || !mDirectoryFile.canWrite())
73              {
74                  Log.printError(this, "Failed to initialize FileDataService, no read/write access on \""+dbDirectoryPath+"\"!");
75                  mDirectoryFile = null;
76              }
77              else
78                  Log.print(this, "FileDataService initialized on path \""+dbDirectoryPath+"\".");
79          }
80          catch(Exception e)
81          {
82              Log.printError(this, "Failed to initialize FileDataService for path \""+dbDirectoryPath+"\"!", e);
83              mDirectoryFile = null;
84          }        
85      }
86      
87      // Superclass overrides ----------------------------------------------------
88      public boolean ping()
89      {
90          return mDirectoryFile!=null;
91      }
92      
93      protected ISelection loadTableSelection(IEntityDescriptor entityDescriptor)
94      {
95          ISelection dbSelection = new Selection(entityDescriptor);
96  
97          if(mDirectoryFile!=null)
98          {
99              try
100             {
101                 File entityFile = new File(mDirectoryFile, entityDescriptor.getCodeName()+".db");
102                 if(entityFile.exists())
103                 {
104                     ObjectInputStream input = new ObjectInputStream(new BufferedInputStream(new FileInputStream(entityFile), 10000));
105                     int entityCount = input.readInt();
106                     for(int j=0; j<entityCount; j++)
107                         dbSelection.addEntity((IEntity)input.readObject());
108                     input.close();
109                 }
110             }
111             catch(Exception e)
112             {
113                 Log.printError(this, "Failed to load complete entity("+entityDescriptor+") file!" , e);
114             }
115         }
116         
117         return dbSelection;
118     }
119     
120     protected void storeTableSelection(ISelection dbSelection)
121     {
122         if(dbSelection!=null && mDirectoryFile!=null)
123         {
124             try
125             {
126                 File entityFile = new File(mDirectoryFile, dbSelection.getEntityDescriptor().getCodeName()+".db");
127                 if(entityFile.exists())
128                     entityFile.delete();
129                     
130                 ObjectOutputStream output = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(entityFile), 10000));
131                 output.writeInt(dbSelection.size());
132                 for(int j=0; j<dbSelection.size(); j++)
133                     output.writeObject(dbSelection.getEntity(j));
134                 output.close();    
135             }
136             catch(Exception e)
137             {
138                 Log.printError(this, "Failed to store complete entity("+dbSelection.getEntityDescriptor()+") file!" , e);
139             }
140         }
141     }    
142 
143     // Access methods ----------------------------------------------------------
144     public File getDirectoryFile()
145     {
146         return mDirectoryFile;
147     }
148     
149 }