View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of Java-Common.
5    *
6    * Java-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Java-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Java-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.application.state;
21  
22  import com.cosylab.application.state.impl.CDBStateStorage;
23  import com.cosylab.application.state.impl.DefaultState;
24  import com.cosylab.application.state.impl.SerialStateStorage;
25  import com.cosylab.application.state.impl.TextStateStorage;
26  import com.cosylab.application.state.impl.XMLStateStorage;
27  
28  import java.io.File;
29  import java.io.FilenameFilter;
30  
31  import java.util.HashMap;
32  import java.util.Iterator;
33  
34  
35  /**
36   * This class is used as the factory for the state exchange objects mainly the
37   * <code>State</code> implementation and the <code>StateStorage</code>
38   * implementation.
39   *
40   * @author dvitas
41   */
42  public final class StateFactory
43  {
44  	/** DOCUMENT ME! */
45  	public static final String STORAGE_BIN = "BIN";
46  
47  	/** DOCUMENT ME! */
48  	public static final String STORAGE_TXT = "TXT";
49  
50  	/** DOCUMENT ME! */
51  	public static final String STORAGE_XML = "XML";
52  
53  	/** DOCUMENT ME! */
54  	public static final String STORAGE_CDB = "CDB";
55  
56  	/** DOCUMENT ME! */
57  	public static final String STORAGE_DAT = "DAT";
58  
59  	/** DOCUMENT ME! */
60  	public static HashMap stateStorages = null;
61  
62  	/** DOCUMENT ME! */
63  	public static String defaultstateStorage = null;
64  
65  	static {
66  		// prepare storage
67  		stateStorages = new HashMap(5);
68  
69  		// register our defaults
70  		registerStateStorage(STORAGE_BIN, SerialStateStorage.class.getName());
71  		registerStateStorage(STORAGE_TXT, TextStateStorage.class.getName());
72  		registerStateStorage(STORAGE_XML, XMLStateStorage.class.getName());
73  		registerStateStorage(STORAGE_CDB, CDBStateStorage.class.getName());
74  		setDefaultStateStorage(STORAGE_XML);
75  
76  		// maybe something else
77  		String userTypeClass = System.getProperty("StateStorage");
78  
79  		if(userTypeClass != null) {
80  			registerStateStorage(STORAGE_DAT, userTypeClass);
81  
82  			// if so set it as default
83  			setDefaultStateStorage(STORAGE_DAT);
84  		}
85  
86  		// default ?
87  		String defaultSS = System.getProperty("DefaultStateStorage");
88  
89  		if(defaultSS != null) {
90  			// if we new it
91  			if(stateStorages.get(defaultSS) != null) {
92  				setDefaultStateStorage(defaultSS);
93  			}
94  		}
95  	}
96  
97  	/**
98  	 * DOCUMENT ME!
99  	 *
100 	 * @param type DOCUMENT ME!
101 	 * @param className DOCUMENT ME!
102 	 */
103 	public static void registerStateStorage(final String type,
104 	    final String className)
105 	{
106 		stateStorages.put(type, className);
107 	}
108 
109 	/**
110 	 * DOCUMENT ME!
111 	 *
112 	 * @param type DOCUMENT ME!
113 	 */
114 	public static void setDefaultStateStorage(final String type)
115 	{
116 		defaultstateStorage = type;
117 	}
118 
119 	protected static boolean typeExists(final File[] files, final String type)
120 	{
121 		if(files == null) {
122 			return false;
123 		}
124 
125 		for(int i = 0; i < files.length; i++) {
126 			if(files[i].getName().endsWith(type)
127 			    || files[i].getName().endsWith(type.toLowerCase())) {
128 				return true;
129 			}
130 		}
131 
132 		return false;
133 	}
134 
135 	/**
136 	 * DOCUMENT ME!
137 	 *
138 	 * @param filePath DOCUMENT ME!
139 	 * @param appName DOCUMENT ME!
140 	 *
141 	 * @return DOCUMENT ME!
142 	 */
143 	public static StateStorage createStateStorage(final String filePath,
144 	    final String appName)
145 	{
146 		// first try to gues state storage type from file extension
147 		try {
148 			// list all files that starts with app name
149 			File file = new File(filePath);
150 			File[] files = file.listFiles(new FilenameFilter() {
151 						public boolean accept(File dir, String name)
152 						{
153 							if(name.startsWith(appName)) {
154 								return true;
155 							}
156 
157 							return false;
158 						}
159 					});
160 
161 			// if there is files and none of them is default then find out which storage to use
162 			if(!typeExists(files, defaultstateStorage)) {
163 				Iterator iter = stateStorages.keySet().iterator();
164 
165 				while(iter.hasNext()) {
166 					String type = (String)iter.next();
167 
168 					if(typeExists(files, type)) {
169 						// try to create it with the delegator
170 						StateStorage ss = createStateStorage(type);
171 						Class[] paramTypes = { StateStorage.class };
172 						Object[] params = { ss };
173 
174 						return (StateStorage)Class.forName((String)stateStorages
175 						    .get(defaultstateStorage)).getConstructor(paramTypes)
176 						.newInstance(params);
177 					}
178 				}
179 			}
180 		} catch(Exception e) {
181 			// no op - will use default
182 		}
183 
184 		return createStateStorage();
185 	}
186 
187 	/**
188 	 * DOCUMENT ME!
189 	 *
190 	 * @return DOCUMENT ME!
191 	 */
192 	public static StateStorage createStateStorage()
193 	{
194 		return createStateStorage(defaultstateStorage);
195 	}
196 
197 	/**
198 	 * DOCUMENT ME!
199 	 *
200 	 * @param type DOCUMENT ME!
201 	 *
202 	 * @return DOCUMENT ME!
203 	 *
204 	 * @throws IllegalArgumentException DOCUMENT ME!
205 	 */
206 	public static StateStorage createStateStorage(final String type)
207 	{
208 		String className = (String)stateStorages.get(type);
209 
210 		if(className == null) {
211 			return null;
212 		}
213 
214 		try {
215 			Object obj = Class.forName(className).newInstance();
216 
217 			return (StateStorage)obj;
218 		} catch(Exception e) {
219 			throw new IllegalArgumentException(e.getMessage());
220 		}
221 	}
222 
223 	/**
224 	 * Creates new storage with default ID.
225 	 *
226 	 * @return no-name storage
227 	 */
228 	public static State createState()
229 	{
230 		return new DefaultState();
231 	}
232 }
233 
234 /* __oOo__ */