KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > backup > pluggable > EnvironmentFactory


1 /*
2  * EnvironmentFactory.java
3  *
4  * Created on December 15, 2003, 10:33 PM
5  */

6
7 package com.sun.enterprise.config.backup.pluggable;
8
9 import com.sun.enterprise.config.backup.BackupAbortException;
10 import com.sun.enterprise.config.backup.utils.LoggerHelper;
11 import com.sun.enterprise.config.backup.utils.LocalStringsHelper;
12 import java.util.logging.Logger JavaDoc;
13 import com.sun.enterprise.config.backup.status.StatusConstants;
14 import com.sun.enterprise.config.backup.pluggable.impl.DefaultBackupEnvironment;
15 import com.sun.enterprise.config.backup.pluggable.impl.DefaultActiveAssistance;
16 import com.sun.enterprise.config.backup.pluggable.impl.OfflineBackupSynchronizer;
17 import com.sun.enterprise.config.backup.pluggable.impl.OnlineBackupSynchronizer;
18 import com.sun.enterprise.config.backup.pluggable.impl.ZipStorage;
19 import com.sun.enterprise.config.backup.pluggable.impl.DirectoryStorage;
20 import com.sun.enterprise.config.backup.BackupStorageMonitor;
21 import com.sun.enterprise.config.backup.HistoryManager;
22 import com.sun.enterprise.config.backup.EnvironmentConstants;
23 import com.sun.enterprise.config.backup.utils.SystemPropsHelper;
24 /**
25  * provides a basic implementation of factory. Users can
26  * extend this class and implement required methods and set their
27  * class in environment variable ENVIRONMENT_FACTORY_CLASS. The
28  * statis create method in this class reads the variable and
29  * instantiates the class. Note that the implemented factory
30  * needs a no arg constructor.
31  *
32  * @author sridatta
33  */

34 public class EnvironmentFactory
35         implements StatusConstants, EnvironmentConstants {
36     
37  
38     private static EnvironmentFactory _ENV = null;
39     private static BackupEnvironment _BE = null;
40     private static ActiveAssistance _AA = null;
41    
42     public static synchronized EnvironmentFactory getEnvironmentFactory() {
43         if(_ENV == null) {
44            _ENV = createEnvironmentFactory();
45         }
46         return _ENV;
47     }
48     
49     private static EnvironmentFactory createEnvironmentFactory() {
50                                             
51         String JavaDoc factoryClassName = SystemPropsHelper.getProperty(ENVIRONMENT_FACTORY_CLASS);
52         
53         Class JavaDoc factoryClass;
54         try {
55             if(factoryClassName!= null && !"".equals(factoryClassName)) {
56                 factoryClass = Class.forName(factoryClassName);
57             } else {
58                 factoryClass = EnvironmentFactory.class;
59             }
60         }catch(Exception JavaDoc e) {
61             throw new BackupAbortException(
62                     "error_loading_environment_factory_class",
63                     LocalStringsHelper.
64                         getString("error_loading_environment_factory_class"),
65                     e);
66         }
67         LoggerHelper.fine("EnvironmentFactory.getEnvironmentFactory():" +
68                     "Factory Class is " + factoryClass);
69         
70         EnvironmentFactory result = null;
71         try {
72             result = (EnvironmentFactory) factoryClass.newInstance();
73         } catch(Exception JavaDoc e) {
74             throw new BackupAbortException(
75                 "error_creating_environment_factory",
76                 LocalStringsHelper.
77                         getString("error_creating_environment_factory"),
78                 e);
79         }
80         
81         return result;
82     }
83     
84     public synchronized BackupEnvironment getBackupEnvironment() {
85         
86         if(_BE == null)
87             _BE = createBackupEnvironment();
88         
89         return _BE;
90     }
91         
92     public synchronized ActiveAssistance getActiveAssistance() {
93         if(_AA == null)
94             _AA = createActiveAssistance();
95         
96         return _AA;
97     }
98         
99      private BackupStorage getBackupStorage(String JavaDoc type) {
100         
101          if(STORAGE_TYPE_ZIP.equals(type)) return new ZipStorage();
102          if(STORAGE_TYPE_DIRECTORY.equals(type)) return new DirectoryStorage();
103          
104          throw new BackupAbortException(
105             "unsupported_storage_type",
106             LocalStringsHelper.getString("unsupported_storage_type", type));
107      }
108      
109     public BackupStorage getBackupStorage() {
110                                     
111         String JavaDoc storageType = getBackupEnvironment().getBackupStorageType();
112         return getBackupStorage(storageType);
113     }
114      
115     protected BackupEnvironment createBackupEnvironment() {
116         return new DefaultBackupEnvironment();
117     }
118     
119     protected ActiveAssistance createActiveAssistance() {
120         return new DefaultActiveAssistance();
121     }
122     
123         
124     public BackupSynchronizer getBackupSynchronizer() {
125                                     
126         String JavaDoc type = getBackupEnvironment().getExecutionType();
127         
128         if(type == TYPE_OFFLINE) return getOfflineBackupSynchronizer();
129         
130         if(type == TYPE_ONLINE) return getOnlineBackupSynchronizer();
131        
132         throw new BackupAbortException(
133             "error_wrong_execution_type",
134             LocalStringsHelper.getString("error_wrong_execution_type",type));
135     }
136     
137     protected BackupSynchronizer getOfflineBackupSynchronizer() {
138         return new OfflineBackupSynchronizer();
139     }
140     
141     protected BackupSynchronizer getOnlineBackupSynchronizer() {
142         return new OnlineBackupSynchronizer();
143     }
144 }
145
Popular Tags