KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > services > monitor > StorageFactoryService


1 /*
2
3    Derby - Class org.apache.derby.impl.services.monitor.StorageFactoryService
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.services.monitor;
23
24 import org.apache.derby.iapi.reference.MessageId;
25 import org.apache.derby.iapi.reference.SQLState;
26
27 import org.apache.derby.iapi.services.i18n.MessageService;
28
29 import org.apache.derby.iapi.services.monitor.Monitor;
30 import org.apache.derby.iapi.services.monitor.PersistentService;
31 import org.apache.derby.iapi.services.sanity.SanityManager;
32
33 import org.apache.derby.iapi.error.StandardException;
34 import org.apache.derby.iapi.store.raw.data.DataFactory;
35
36 import org.apache.derby.io.StorageFile;
37 import org.apache.derby.io.StorageFactory;
38 import org.apache.derby.io.WritableStorageFactory;
39
40 import org.apache.derby.iapi.reference.Attribute;
41 import org.apache.derby.iapi.reference.Property;
42
43 import java.io.File JavaDoc;
44 import java.io.FileInputStream JavaDoc;
45 import java.io.FileOutputStream JavaDoc;
46 import java.io.InputStream JavaDoc;
47 import java.io.BufferedInputStream JavaDoc;
48 import java.io.OutputStream JavaDoc;
49 import java.io.IOException JavaDoc;
50 import java.io.FileNotFoundException JavaDoc;
51
52 import java.util.Enumeration JavaDoc;
53 import java.util.NoSuchElementException JavaDoc;
54 import java.util.Properties JavaDoc;
55
56 import java.security.AccessController JavaDoc;
57 import java.security.PrivilegedAction JavaDoc;
58 import java.security.PrivilegedExceptionAction JavaDoc;
59 import java.security.PrivilegedActionException JavaDoc;
60
61 /**
62  * This class implements the PersistentService interface using a StorageFactory class.
63  * It handles all subSubProtocols except for cache.
64  */

65 final class StorageFactoryService implements PersistentService
66 {
67
68     private String JavaDoc home; // the path of the database home directory. Can be null
69
private String JavaDoc canonicalHome; // will be null if home is null
70
private final String JavaDoc subSubProtocol;
71     private final Class JavaDoc storageFactoryClass;
72     private StorageFactory rootStorageFactory;
73     private char separatorChar;
74
75     StorageFactoryService( String JavaDoc subSubProtocol, Class JavaDoc storageFactoryClass)
76         throws StandardException
77     {
78         this.subSubProtocol = subSubProtocol;
79         this.storageFactoryClass = storageFactoryClass;
80
81         Object JavaDoc monitorEnv = Monitor.getMonitor().getEnvironment();
82         if (monitorEnv instanceof File)
83         {
84             final File relativeRoot = (File) monitorEnv;
85             try
86             {
87                 AccessController.doPrivileged(
88                     new java.security.PrivilegedExceptionAction JavaDoc()
89                     {
90                         public Object JavaDoc run() throws IOException JavaDoc, StandardException
91                         {
92                             home = relativeRoot.getPath();
93                             canonicalHome = relativeRoot.getCanonicalPath();
94                             rootStorageFactory = getStorageFactoryInstance( true, null, null, null);
95                             if( home != null)
96                             {
97                                 StorageFile rootDir = rootStorageFactory.newStorageFile( null);
98                                 rootDir.mkdirs();
99                             }
100                             return null;
101                         }
102                     }
103                     );
104             }
105             catch( PrivilegedActionException JavaDoc pae)
106             {
107                 home = null;
108                 canonicalHome = null;
109             }
110         }
111         if( rootStorageFactory == null)
112         {
113             try
114             {
115                 rootStorageFactory = getStorageFactoryInstance( true, null, null, null);
116             }
117             catch( IOException JavaDoc ioe){ throw Monitor.exceptionStartingModule(/*serviceName, */ ioe); }
118         }
119         AccessController.doPrivileged(
120             new java.security.PrivilegedAction JavaDoc()
121             {
122                 public Object JavaDoc run()
123                 {
124                     separatorChar = rootStorageFactory.getSeparator();
125                     return null;
126                 }
127             }
128             );
129     } // end of constructor
130

131     
132     /*
133     ** Methods of PersistentService
134     */

135
136     /**
137      * @return true if the PersistentService has a StorageFactory, false if not.
138      */

139     public boolean hasStorageFactory()
140     {
141         return true;
142     }
143     
144     /**
145      * Get an initialized StorageFactoryInstance
146      *
147      * @param useHome If true and the database name is not absolute then the database directory will be
148      * relative to the home directory, if one is defined in the properties file.
149      * @param databaseName The name of the database (directory). The name does not include the subSubProtocol.
150      * If null then the storage factory will only be used to deal with the directory containing
151      * the databases.
152      * @param tempDirName The name of the temporary file directory set in properties. If null then a default
153      * directory should be used. Each database should get a separate temporary file
154      * directory within this one to avoid collisions.
155      * @param uniqueName A unique name that can be used to create the temporary file directory for this database.
156      * If null then temporary files will not be created in this StorageFactory instance.
157      *
158      * @return An initialized StorageFactory.
159      *
160      * @exception IOException if create, the database directory does not exist, and it cannot be created;
161      * if !create and the database does not exist as a directory.
162      */

163     public StorageFactory getStorageFactoryInstance(final boolean useHome,
164                                                     final String JavaDoc databaseName,
165                                                     final String JavaDoc tempDirName,
166                                                     final String JavaDoc uniqueName)
167         throws StandardException, IOException JavaDoc
168     {
169         try
170         {
171             return (StorageFactory) AccessController.doPrivileged(
172                 new PrivilegedExceptionAction JavaDoc()
173                 {
174                     public Object JavaDoc run() throws InstantiationException JavaDoc, IllegalAccessException JavaDoc, IOException JavaDoc
175                     {
176                         return privGetStorageFactoryInstance( useHome, databaseName, tempDirName, uniqueName);
177                     }
178                 });
179         }
180         catch (PrivilegedActionException JavaDoc pae)
181         {
182             Exception JavaDoc e = pae.getException();
183             throw StandardException.newException( SQLState.REGISTERED_CLASS_INSTANCE_ERROR,
184                                                   e, subSubProtocol, storageFactoryClass);
185         }
186     } // end of getStorageFactoryInstance
187

188     private StorageFactory privGetStorageFactoryInstance( boolean useHome,
189                                                           String JavaDoc databaseName,
190                                                           String JavaDoc tempDirName,
191                                                           String JavaDoc uniqueName)
192          throws InstantiationException JavaDoc, IllegalAccessException JavaDoc, IOException JavaDoc
193     {
194         StorageFactory storageFactory = (StorageFactory) storageFactoryClass.newInstance();
195         String JavaDoc dbn;
196         if( databaseName != null
197             && subSubProtocol != null
198             && databaseName.startsWith( subSubProtocol + ":"))
199             dbn = databaseName.substring( subSubProtocol.length() + 1);
200         else
201             dbn = databaseName;
202         storageFactory.init( useHome ? home : null, dbn, tempDirName, uniqueName);
203         return storageFactory;
204     } // end of privGetStorageFactoryInstance
205

206     /**
207         The type of the service is 'directory'
208
209         @see PersistentService#getType
210     */

211     public String JavaDoc getType()
212     {
213         return subSubProtocol;
214     }
215
216
217     /**
218         Return a list of all the directoies in the system directory.
219
220         @see PersistentService#getBootTimeServices
221     */

222     public Enumeration JavaDoc getBootTimeServices()
223     {
224         if( home == null)
225             return null;
226         return new DirectoryList();
227     }
228
229     /**
230         Open the service properties in the directory identified by the service name.
231
232         The property SERVICE_ROOT (db2j.rt.serviceRoot) is added
233         by this method and set to the service directory.
234
235         @return A Properties object or null if serviceName does not represent a valid service.
236
237         @exception StandardException Service appears valid but the properties cannot be created.
238     */

239     public Properties JavaDoc getServiceProperties( final String JavaDoc serviceName, Properties JavaDoc defaultProperties)
240         throws StandardException
241     {
242         if (SanityManager.DEBUG) {
243             if (! serviceName.equals(getCanonicalServiceName(serviceName)))
244             {
245                 SanityManager.THROWASSERT("serviceName (" + serviceName +
246                                           ") expected to equal getCanonicalServiceName(serviceName) (" +
247                                           getCanonicalServiceName(serviceName) + ")");
248             }
249         }
250
251         //recreate the service root if requested by the user.
252
final String JavaDoc recreateFrom = recreateServiceRoot(serviceName, defaultProperties);
253
254         InputStream JavaDoc is = null;
255         try
256         {
257             is = (InputStream JavaDoc) AccessController.doPrivileged(
258                 new PrivilegedExceptionAction JavaDoc()
259                 {
260                     public Object JavaDoc run()
261                         throws FileNotFoundException JavaDoc, IOException JavaDoc, StandardException,
262                         InstantiationException JavaDoc, IllegalAccessException JavaDoc
263                     {
264                         if( recreateFrom != null) // restore from a file
265
{
266                             File propFile = new File(recreateFrom, PersistentService.PROPERTIES_NAME);
267                             return new FileInputStream JavaDoc(propFile);
268                         }
269                         else
270                         {
271                             StorageFactory storageFactory = privGetStorageFactoryInstance( true, serviceName, null, null);
272                             StorageFile file = storageFactory.newStorageFile( PersistentService.PROPERTIES_NAME);
273                             InputStream JavaDoc is1 = file.getInputStream();
274                             storageFactory.shutdown();
275                             return is1;
276                         }
277                     }
278                 }
279                 );
280
281             Properties JavaDoc serviceProperties = new Properties JavaDoc(defaultProperties);
282             serviceProperties.load(new BufferedInputStream JavaDoc(is));
283
284             return serviceProperties;
285         }
286         catch (PrivilegedActionException JavaDoc pae)
287         {
288             if( pae.getException() instanceof FileNotFoundException JavaDoc)
289                 return null;
290             throw Monitor.exceptionStartingModule( pae.getException());
291         }
292         catch (FileNotFoundException JavaDoc fnfe) {return null ;}
293         catch (SecurityException JavaDoc se) { throw Monitor.exceptionStartingModule(/*serviceName, */ se); }
294         catch (IOException JavaDoc ioe) { throw Monitor.exceptionStartingModule(/*serviceName, */ ioe); }
295         finally
296         {
297             if (is != null)
298             {
299                 try
300                 {
301                     is.close();
302                 }
303                 catch (IOException JavaDoc ioe2) {}
304             }
305         }
306     } // end of getServiceProperties
307

308
309     /**
310         @exception StandardException Properties cannot be saved.
311     */

312
313     public void saveServiceProperties( final String JavaDoc serviceName,
314                                        StorageFactory sf,
315                                        final Properties JavaDoc properties,
316                                        final boolean replace)
317         throws StandardException
318     {
319         if (SanityManager.DEBUG)
320         {
321             SanityManager.ASSERT(serviceName.equals(getCanonicalServiceName(serviceName)), serviceName);
322         }
323         if( ! (sf instanceof WritableStorageFactory))
324             throw StandardException.newException(SQLState.READ_ONLY_SERVICE);
325         final WritableStorageFactory storageFactory = (WritableStorageFactory) sf;
326         try
327         {
328             AccessController.doPrivileged(
329                 new PrivilegedExceptionAction JavaDoc()
330                 {
331                     public Object JavaDoc run() throws StandardException
332                     {
333                         StorageFile backupFile = null;
334                         StorageFile servicePropertiesFile = storageFactory.newStorageFile( PersistentService.PROPERTIES_NAME);
335
336                         if (replace)
337                         {
338                             backupFile = storageFactory.newStorageFile( PersistentService.PROPERTIES_NAME.concat("old"));
339                             try
340                             {
341                                 if(!servicePropertiesFile.renameTo(backupFile))
342                                     throw StandardException.newException(SQLState.UNABLE_TO_RENAME_FILE,
343                                                                          servicePropertiesFile, backupFile);
344                             }
345                             catch (SecurityException JavaDoc se) { throw Monitor.exceptionStartingModule(se); }
346                         }
347
348                         OutputStream JavaDoc os = null;
349                         try
350                         {
351                             os = servicePropertiesFile.getOutputStream();
352                             properties.store( os, serviceName + MessageService.getTextMessage(MessageId.SERVICE_PROPERTIES_DONT_EDIT));
353                             storageFactory.sync( os, false);
354                             os.close();
355                             os = null;
356                         }
357                         catch (IOException JavaDoc ioe)
358                         {
359                             if (os != null)
360                             {
361                                 try
362                                 {
363                                     os.close();
364                                 }
365                                 catch (IOException JavaDoc ioe2) {}
366                                 os = null;
367                             }
368
369                             if (backupFile != null)
370                             {
371                                 // need to re-name the old properties file back again
372
try
373                                 {
374                                     servicePropertiesFile.delete();
375                                     backupFile.renameTo(servicePropertiesFile);
376                                 }
377                                 catch (SecurityException JavaDoc se) {}
378                             }
379                             throw Monitor.exceptionStartingModule(ioe);
380                         }
381         
382                         if (backupFile != null)
383                         {
384                             try
385                             {
386                                 backupFile.delete();
387                                 backupFile = null;
388                             }
389                             catch (SecurityException JavaDoc se) {}
390                         }
391                         return null;
392                     }
393                 }
394                 );
395         }
396         catch( PrivilegedActionException JavaDoc pae) { throw (StandardException) pae.getException();}
397     } // end of saveServiceProperties
398

399     /**
400        Save to a backup file
401        
402         @exception StandardException Properties cannot be saved.
403     */

404
405     public void saveServiceProperties(final String JavaDoc serviceName,
406                                       final Properties JavaDoc properties,
407                                       final boolean replace)
408         throws StandardException {
409
410         try
411         {
412             AccessController.doPrivileged(
413                 new PrivilegedExceptionAction JavaDoc()
414                 {
415                     File backupFile = null;
416                     public Object JavaDoc run() throws StandardException
417                     {
418
419                         File servicePropertiesFile =
420                             new File(serviceName, PersistentService.PROPERTIES_NAME);
421                         if (replace) {
422                             backupFile =
423                                 new File(serviceName, PersistentService.PROPERTIES_NAME.concat("old"));
424                             try {
425                                 if(!servicePropertiesFile.renameTo(backupFile)) {
426                                     throw StandardException.newException(
427                                      SQLState.UNABLE_TO_RENAME_FILE, servicePropertiesFile, backupFile);
428                                 }
429                             } catch (SecurityException JavaDoc se) {
430                                 throw Monitor.exceptionStartingModule(se);
431                             }
432                         }
433
434                         FileOutputStream JavaDoc fos = null;
435                         try {
436
437                             fos = new FileOutputStream JavaDoc(servicePropertiesFile);
438                             properties.store(fos,
439                                              serviceName +
440                                              MessageService.getTextMessage(
441                                                   MessageId.SERVICE_PROPERTIES_DONT_EDIT));
442                             fos.getFD().sync();
443                             fos.close();
444                             fos = null;
445                         } catch (IOException JavaDoc ioe) {
446
447                             if (fos != null) {
448                                 try {
449                                     fos.close();
450                                 } catch (IOException JavaDoc ioe2) {
451                                 }
452                                 fos = null;
453                             }
454
455                             if (backupFile != null) {
456                                 // need to re-name the old properties file back again
457
try {
458                                     servicePropertiesFile.delete();
459                                     backupFile.renameTo(servicePropertiesFile);
460                                 } catch (SecurityException JavaDoc se) {
461                                 }
462                             }
463                             throw Monitor.exceptionStartingModule(ioe);
464                         }
465         
466         
467                         if (backupFile != null) {
468                             try {
469                                 backupFile.delete();
470                                 backupFile = null;
471                             } catch (SecurityException JavaDoc se) {
472                                 // do nothing
473
}
474                         }
475                         return null;
476                     }
477                 }
478                 );
479         }catch( PrivilegedActionException JavaDoc pae) { throw (StandardException) pae.getException();}
480     }
481                 
482     /*
483     **Recreates service root if required depending on which of the following
484     **attribute is specified on the conection URL:
485     ** Attribute.CREATE_FROM (Create database from backup if it does not exist):
486     ** When a database not exist, the service(database) root is created
487     ** and the PersistentService.PROPERTIES_NAME (service.properties) file
488     ** is restored from the backup.
489     ** Attribute.RESTORE_FROM (Delete the whole database if it exists and then restore
490     ** it from backup)
491     ** Existing database root is deleted and the new the service(database) root is created.
492     ** PersistentService.PROPERTIES_NAME (service.properties) file is restored from the backup.
493     ** Attribute.ROLL_FORWARD_RECOVERY_FROM:(Perform Rollforward Recovery;
494     ** except for the log directory everthing else is replced by the copy from
495     ** backup. log files in the backup are copied to the existing online log
496     ** directory.):
497     ** When a database not exist, the service(database) root is created.
498     ** PersistentService.PROPERTIES_NAME (service.properties) file is deleted
499     ** from the service dir and recreated with the properties from backup.
500     */

501
502     protected String JavaDoc recreateServiceRoot( final String JavaDoc serviceName,
503                                           Properties JavaDoc properties) throws StandardException
504     {
505         //if there are no propertues then nothing to do in this routine
506
if(properties == null) {
507             return null;
508         }
509
510         String JavaDoc restoreFrom; //location where backup copy of service properties available
511
boolean createRoot = false;
512         boolean deleteExistingRoot = false;
513         
514         //check if user wants to create a database from a backup copy
515
restoreFrom = properties.getProperty(Attribute.CREATE_FROM);
516         if(restoreFrom !=null)
517         {
518             //create root dicretory if it does not exist.
519
createRoot =true;
520             deleteExistingRoot = false;
521         }
522         else
523         { //check if user requested a complete restore(version recovery) from backup
524
restoreFrom = properties.getProperty(Attribute.RESTORE_FROM);
525             //create root dir if it does not exists and if there exists one already delete and recreate
526
if(restoreFrom !=null)
527             {
528                 createRoot =true;
529                 deleteExistingRoot = true;
530             }
531             else
532             {
533                 //check if user has requested roll forward recovery using a backup
534
restoreFrom = properties.getProperty(Attribute.ROLL_FORWARD_RECOVERY_FROM);
535                 if(restoreFrom !=null)
536                 {
537                     //if service root does not exist then only create one
538
//This is useful when logDevice was on some other device
539
//and the device on which data directorties existied has
540
//failed and user is trying to restore it some other device.
541
try
542                     {
543                         if( AccessController.doPrivileged(
544                                 new PrivilegedExceptionAction JavaDoc()
545                                 {
546                                     public Object JavaDoc run()
547                                         throws IOException JavaDoc, StandardException, InstantiationException JavaDoc, IllegalAccessException JavaDoc
548                                     {
549                                         StorageFactory storageFactory
550                                           = privGetStorageFactoryInstance( true, serviceName, null, null);
551                                         try
552                                         {
553                                             StorageFile serviceDirectory = storageFactory.newStorageFile( null);
554                                             return serviceDirectory.exists() ? this : null;
555                                         }
556                                         finally {storageFactory.shutdown();}
557                                     }
558                                 }
559                                 ) == null)
560                         {
561                             createRoot =true;
562                             deleteExistingRoot = false;
563                         }
564                         
565                     }
566                     catch( PrivilegedActionException JavaDoc pae)
567                     {
568                         throw Monitor.exceptionStartingModule( (IOException JavaDoc) pae.getException());
569                     }
570                 }
571             }
572         }
573
574         //restore the service properties from backup
575
if(restoreFrom != null)
576         {
577             //First make sure backup service directory exists in the specified path
578
File backupRoot = new File(restoreFrom);
579             if(backupRoot.exists())
580             {
581                 //First make sure backup have service.properties
582
File bserviceProp = new File(restoreFrom, PersistentService.PROPERTIES_NAME);
583                 if(bserviceProp.exists())
584                 {
585                     //create service root if required
586
if(createRoot)
587                         createServiceRoot(serviceName, deleteExistingRoot);
588                     try
589                     {
590                         AccessController.doPrivileged(
591                             new PrivilegedExceptionAction JavaDoc()
592                             {
593                                 public Object JavaDoc run()
594                                     throws IOException JavaDoc, StandardException, InstantiationException JavaDoc, IllegalAccessException JavaDoc
595                                 {
596                                     WritableStorageFactory storageFactory =
597                                       (WritableStorageFactory) privGetStorageFactoryInstance( true,
598                                                                                               serviceName,
599                                                                                               null,
600                                                                                               null);
601                                     try
602                                     {
603                                         StorageFile cserviceProp = storageFactory.newStorageFile( PersistentService.PROPERTIES_NAME);
604
605                                         if(cserviceProp.exists())
606                                             if(!cserviceProp.delete())
607                                                 throw StandardException.newException(SQLState.UNABLE_TO_DELETE_FILE,
608                                                                                      cserviceProp);
609                                         return null;
610                                     }
611                                     finally { storageFactory.shutdown();}
612                                 }
613                             }
614                             );
615                     }
616                     catch( PrivilegedActionException JavaDoc pae)
617                     { throw Monitor.exceptionStartingModule( (IOException JavaDoc)pae.getException());}
618                 }
619                 else
620                     throw StandardException.newException(SQLState.PROPERTY_FILE_NOT_FOUND_IN_BACKUP, bserviceProp);
621             }
622             else
623                 throw StandardException.newException(SQLState.SERVICE_DIRECTORY_NOT_IN_BACKUP, backupRoot);
624
625             properties.put(Property.IN_RESTORE_FROM_BACKUP,"True");
626             if(createRoot)
627                 properties.put(Property.DELETE_ROOT_ON_ERROR, "True");
628         }
629         return restoreFrom;
630     } // end of recreateServiceRoot
631

632     /**
633         Properties cannot be saved
634     */

635     public String JavaDoc createServiceRoot(final String JavaDoc name, final boolean deleteExisting)
636         throws StandardException
637     {
638         if( !( rootStorageFactory instanceof WritableStorageFactory))
639             throw StandardException.newException(SQLState.READ_ONLY_SERVICE);
640         // we need to create the directory before we can call
641
// getCanonicalPath() on it, because if intermediate directories
642
// need to be created the getCanonicalPath() will fail.
643

644         Throwable JavaDoc t = null;
645         try
646         {
647             String JavaDoc protocolLeadIn = "";
648             //prepend the subsub protocol name to the storage factoty canonical
649
//name to form the service name except in case of the the
650
//default subsubprototcol(PersistentService.DIRECTORY)
651

652             if (!(getType().equals( PersistentService.DIRECTORY)))
653                 protocolLeadIn = getType() + ":";
654
655             return protocolLeadIn + (String JavaDoc) AccessController.doPrivileged(
656                 new PrivilegedExceptionAction JavaDoc()
657                 {
658                     public Object JavaDoc run()
659                         throws StandardException, IOException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc
660                     {
661                         StorageFactory storageFactory = privGetStorageFactoryInstance( true, name, null, null);
662                         try
663                         {
664                             StorageFile serviceDirectory = storageFactory.newStorageFile( null);
665
666                             if (serviceDirectory.exists())
667                             {
668                                 if (deleteExisting)
669                                 {
670                                     if (!serviceDirectory.deleteAll())
671                                         throw StandardException.newException(SQLState.SERVICE_DIRECTORY_REMOVE_ERROR,
672                                                                              getDirectoryPath( name));
673                                 }
674                                 else
675                                     throw StandardException.newException(SQLState.SERVICE_DIRECTORY_EXISTS_ERROR,
676                                                                          getDirectoryPath( name));
677                             }
678
679                             if (serviceDirectory.mkdirs())
680                             {
681                                 try
682                                 {
683                                     return storageFactory.getCanonicalName();
684                                 }
685                                 catch (IOException JavaDoc ioe)
686                                 {
687                                     serviceDirectory.deleteAll();
688                                     throw ioe;
689                                 }
690                             }
691                             throw StandardException.newException(SQLState.SERVICE_DIRECTORY_CREATE_ERROR, serviceDirectory);
692                         }
693                         finally { storageFactory.shutdown(); }
694                     }
695                 }
696                 );
697         }
698         catch (SecurityException JavaDoc se) { t = se; }
699         catch (PrivilegedActionException JavaDoc pae)
700         {
701             t = pae.getException();
702             if( t instanceof StandardException)
703                 throw (StandardException) t;
704         }
705
706         throw StandardException.newException(SQLState.SERVICE_DIRECTORY_CREATE_ERROR, t, name);
707     } // end of createServiceRoot
708

709     private String JavaDoc getDirectoryPath( String JavaDoc name)
710     {
711         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
712         if( home != null)
713         {
714             sb.append( home);
715             sb.append( separatorChar);
716         }
717         if( separatorChar != '/')
718             sb.append( name.replace( '/', separatorChar));
719         else
720             sb.append( name);
721         return sb.toString();
722     } // end of getDirectoryPath
723

724     public boolean removeServiceRoot(final String JavaDoc serviceName)
725     {
726         if( !( rootStorageFactory instanceof WritableStorageFactory))
727             return false;
728         try
729         {
730             return AccessController.doPrivileged(
731                 new PrivilegedExceptionAction JavaDoc()
732                 {
733                     public Object JavaDoc run()
734                         throws StandardException, IOException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc
735                     {
736                         StorageFactory storageFactory = privGetStorageFactoryInstance( true, serviceName, null, null);
737                         try
738                         {
739                             if (SanityManager.DEBUG)
740                                 SanityManager.ASSERT(serviceName.equals( storageFactory.getCanonicalName()), serviceName);
741                             StorageFile serviceDirectory = storageFactory.newStorageFile( null);
742                             return serviceDirectory.deleteAll() ? this : null;
743                         }
744                         finally { storageFactory.shutdown(); }
745                     }
746                 }
747                 ) != null;
748         }
749         catch( PrivilegedActionException JavaDoc pae){ return false;}
750     } // end of removeServiceRoot
751

752     public String JavaDoc getCanonicalServiceName(String JavaDoc name)
753         throws StandardException
754     {
755         String JavaDoc protocolLeadIn = getType() + ":";
756         int colon = name.indexOf( ':');
757         if( colon > 1) // Subsubprotocols must be at least 2 characters long
758
{
759             if( ! name.startsWith( protocolLeadIn))
760                 return null; // It is not our database
761
name = name.substring( colon + 1);
762         }
763         if( getType().equals( PersistentService.DIRECTORY)) // The default subsubprototcol
764
protocolLeadIn = "";
765         final String JavaDoc nm = name;
766
767         try
768         {
769             return protocolLeadIn + (String JavaDoc) AccessController.doPrivileged(
770                 new PrivilegedExceptionAction JavaDoc()
771                 {
772                     public Object JavaDoc run()
773                         throws StandardException, IOException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc
774                     {
775                         StorageFactory storageFactory = privGetStorageFactoryInstance( true, nm, null, null);
776                         try
777                         {
778                             return storageFactory.getCanonicalName();
779                         }
780                         finally { storageFactory.shutdown();}
781                     }
782                 }
783                 );
784         }
785         catch (PrivilegedActionException JavaDoc pae)
786         {
787             throw Monitor.exceptionStartingModule(pae.getException());
788         }
789     } // end of getCanonicalServiceName
790

791     public String JavaDoc getUserServiceName(String JavaDoc serviceName)
792     {
793         if (home != null)
794         {
795             // allow for file separatorChar by adding 1 to the length
796
if ((serviceName.length() > (canonicalHome.length() + 1)) && serviceName.startsWith(canonicalHome))
797             {
798                 serviceName = serviceName.substring(canonicalHome.length());
799                 if (serviceName.charAt(0) == separatorChar)
800                     serviceName = serviceName.substring(1);
801             }
802         }
803
804         return serviceName.replace( separatorChar, '/');
805     }
806
807     public boolean isSameService(String JavaDoc serviceName1, String JavaDoc serviceName2)
808     {
809         if (SanityManager.DEBUG)
810         {
811             try {
812             SanityManager.ASSERT(serviceName1.equals(getCanonicalServiceName(serviceName1)), serviceName1);
813             SanityManager.ASSERT(serviceName2.equals(getCanonicalServiceName(serviceName2)), serviceName2);
814             } catch (StandardException se)
815             {
816                 return false;
817             }
818         }
819         return serviceName1.equals(serviceName2);
820     } // end of isSameService
821

822
823     /**
824      * Get the StorageFactory implementation for this PersistentService
825      *
826      * @return the StorageFactory class.
827      */

828     public Class JavaDoc getStorageFactoryClass()
829     {
830         return storageFactoryClass;
831     }
832     
833     final class DirectoryList implements Enumeration JavaDoc, PrivilegedAction JavaDoc
834     {
835         private String JavaDoc[] contents;
836         private StorageFile systemDirectory;
837
838         private int index;
839         private boolean validIndex;
840
841         private int actionCode;
842         private static final int INIT_ACTION = 0;
843         private static final int HAS_MORE_ELEMENTS_ACTION = 1;
844
845         DirectoryList()
846         {
847             actionCode = INIT_ACTION;
848             AccessController.doPrivileged( this);
849         }
850
851         public boolean hasMoreElements()
852         {
853             if (validIndex)
854                 return true;
855
856             actionCode = HAS_MORE_ELEMENTS_ACTION;
857             return AccessController.doPrivileged( this) != null;
858         } // end of hasMoreElements
859

860         public Object JavaDoc nextElement() throws NoSuchElementException JavaDoc
861         {
862             if (!hasMoreElements())
863                 throw new NoSuchElementException JavaDoc();
864
865             validIndex = false;
866             return contents[index++];
867         } // end of nextElement
868

869         // PrivilegedAction method
870
public final Object JavaDoc run()
871         {
872             switch( actionCode)
873             {
874             case INIT_ACTION:
875                 systemDirectory = rootStorageFactory.newStorageFile( null);
876                 contents = systemDirectory.list();
877                 return null;
878
879             case HAS_MORE_ELEMENTS_ACTION:
880                 for (; index < contents.length; contents[index++] = null)
881                 {
882                     try
883                     {
884                         StorageFactory storageFactory = privGetStorageFactoryInstance( true, contents[index], null, null);
885                         try
886                         {
887                             StorageFile properties = storageFactory.newStorageFile( PersistentService.PROPERTIES_NAME);
888                             if (!properties.exists())
889                                 continue;
890                             // convert to a canonical name while we are here.
891
contents[index] = storageFactory.getCanonicalName();
892                             validIndex = true;
893
894                             return this;
895                         }
896                         finally { storageFactory.shutdown(); }
897                     }
898                     catch (Exception JavaDoc se) { continue; }
899                 }
900                 return null;
901             }
902             return null;
903         } // end of run
904
} // end of class DirectoryList
905
}
906
Popular Tags