1
15
16 package gate;
17
18 import java.io.Serializable;
19 import java.net.URL;
20 import java.util.*;
21
22 import gate.creole.*;
23 import gate.event.CreoleEvent;
24 import gate.event.CreoleListener;
25 import gate.persist.PersistenceException;
26 import gate.persist.SerialDataStore;
27 import gate.security.*;
28 import gate.security.SecurityException;
29 import gate.util.*;
30
31
33 public abstract class Factory {
34
35 private static final boolean DEBUG = false;
36
37
38 private static CreoleRegister reg = Gate.getCreoleRegister();
39
40
41 private static DataStoreRegister dsReg = Gate.getDataStoreRegister();
42
43
44 private static CreoleProxy creoleProxy;
45
46
47 private static HashMap accessControllerPool;
48
49
52 public static Resource createResource(String resourceClassName)
53 throws ResourceInstantiationException
54 {
55 ResourceData resData = (ResourceData) reg.get(resourceClassName);
57 if(resData == null)
58 throw new ResourceInstantiationException(
59 "Couldn't get resource data for " + resourceClassName
60 );
61
62 ParameterList paramList = resData.getParameterList();
64 FeatureMap parameterValues = null;
65 try {
66 parameterValues = paramList.getInitimeDefaults();
67 } catch(ParameterException e) {
68 throw new ResourceInstantiationException(
69 "Couldn't get default parameters for " + resourceClassName + ": " + e
70 );
71 }
72
73 return createResource(resourceClassName, parameterValues);
74 }
76
87 public static Resource createResource(
88 String resourceClassName, FeatureMap parameterValues
89 ) throws ResourceInstantiationException
90 {
91 return createResource(resourceClassName, parameterValues, null, null);
92 }
94
106 public static Resource createResource(
107 String resourceClassName, FeatureMap parameterValues,
108 FeatureMap features
109 ) throws ResourceInstantiationException
110 {
111 return createResource(resourceClassName, parameterValues,
112 features, null);
113 }
114
115
130 public static Resource createResource(
131 String resourceClassName, FeatureMap parameterValues,
132 FeatureMap features, String resourceName
133 ) throws ResourceInstantiationException
134 {
135 ResourceData resData = (ResourceData) reg.get(resourceClassName);
137 if(resData == null)
138 throw new ResourceInstantiationException(
139 "Couldn't get resource data for " + resourceClassName
140 );
141 Class resClass = null;
143 try {
144 resClass = resData.getResourceClass();
145 } catch(ClassNotFoundException e) {
146 throw new ResourceInstantiationException(
147 "Couldn't get resource class from the resource data:"+Strings.getNl()+e
148 );
149 }
150
151 Resource res = null;
153
154 DataStore dataStore;
156 if(LanguageResource.class.isAssignableFrom(resClass) &&
157 ((dataStore = (DataStore)parameterValues.
158 get(DataStore.DATASTORE_FEATURE_NAME)) != null)
159 ){
160 if(dataStore instanceof SerialDataStore) {
162 if(! (resClass instanceof Serializable))
164 throw new ResourceInstantiationException(
165 "Resource cannot be (de-)serialized: " + resClass.getName()
166 );
167 }
168
169 Object instanceId = parameterValues.get(DataStore.LR_ID_FEATURE_NAME);
171 if(instanceId == null)
172 throw new
173 ResourceInstantiationException("No instance id for " + resClass);
174 try {
175 res = dataStore.getLr(resClass.getName(), instanceId);
176 } catch(PersistenceException pe) {
177 throw new ResourceInstantiationException("Bad read from DB: " + pe);
178 } catch(SecurityException se) {
179 throw new ResourceInstantiationException("Insufficient permissions: " + se);
180 }
181 resData.addInstantiation(res);
182 if(features != null){
183 if(res.getFeatures() == null){
184 res.setFeatures(newFeatureMap());
185 }
186 res.getFeatures().putAll(features);
187 }
188
189 if(res.getName() == null){
191 res.setName(resourceName == null ?
192 resData.getName() + "_" + Gate.genSym() :
193 resourceName);
194 }
195
196 creoleProxy.fireResourceLoaded(
198 new CreoleEvent(res, CreoleEvent.RESOURCE_LOADED)
199 );
200
201 return res;
202 }
203
204
206 try {
208 if(DEBUG) Out.prln("Creating resource " + resClass.getName());
209 res = (Resource) resClass.newInstance();
210 } catch(IllegalAccessException e) {
211 throw new ResourceInstantiationException(
212 "Couldn't create resource instance, access denied: " + e
213 );
214 } catch(InstantiationException e) {
215 throw new ResourceInstantiationException(
216 "Couldn't create resource instance due to newInstance() failure: " + e
217 );
218 }
219
220 if(resourceName == null){
222 resourceName = resData.getName() + "_" + Gate.genSym();
223 }
224 res.setName(resourceName);
225
226 if(LanguageResource.class.isAssignableFrom(resClass)) {
227 if(DEBUG) Out.prln(resClass.getName() + " is a LR");
229 } else if(ProcessingResource.class.isAssignableFrom(resClass)) {
230 if(DEBUG) Out.prln(resClass.getName() + " is a PR");
232 try{
234 FeatureMap parameters = newFeatureMap();
235 parameters.putAll(resData.getParameterList().getRuntimeDefaults());
236 res.setParameterValues(parameters);
237 }catch(ParameterException pe){
238 throw new ResourceInstantiationException(
239 "Could not set the runtime parameters " +
240 "to their default values for: " + res.getClass().getName() +
241 " :\n" + pe.toString()
242 );
243 }
244 } else if(VisualResource.class.isAssignableFrom(resClass)) {
246 if(DEBUG) Out.prln(resClass.getName() + " is a VR");
247
248 } else if(Controller.class.isAssignableFrom(resClass)){
250 } else {
252 Err.prln("WARNING: instantiating resource which is not a PR, LR or VR:");
253 Err.prln(resData + "END OF WARNING" + Strings.getNl());
254 }
255
256
257
258 try{
260 FeatureMap parameters = newFeatureMap();
261 parameters.putAll(resData.getParameterList().getInitimeDefaults());
263 parameters.putAll(parameterValues);
265 res.setParameterValues(parameters);
266 }catch(ParameterException pe){
267 throw new ResourceInstantiationException(
268 "Could not set the init parameters for: " +
269 res.getClass().getName() + " :\n" + pe.toString()
270 );
271 }
272
273 Map listeners = new HashMap(gate.gui.MainFrame.getListeners());
274 if(listeners != null && !listeners.isEmpty()) {
276 try {
277 if(DEBUG) Out.prln("Setting the listeners for " + res.toString());
278 AbstractResource.setResourceListeners(res, listeners);
279 } catch(Exception e) {
280 if(DEBUG) Out.prln("Failed to set listeners for " + res.toString());
281 throw new
282 ResourceInstantiationException("Parameterisation failure" + e);
283 }
284 }
285
286 if(res.getFeatures() == null || res.getFeatures().isEmpty()){
289 FeatureMap fm = newFeatureMap();
290 fm.putAll(resData.getFeatures());
291 res.setFeatures(fm);
292 }
293
294 if(DEBUG) Out.prln("Initialising resource " + res.toString());
296 res = res.init();
297
298 if(listeners != null && !listeners.isEmpty()) {
300 try {
301 if(DEBUG) Out.prln("Removing the listeners for " + res.toString());
302 AbstractResource.removeResourceListeners(res, listeners);
303 } catch(Exception e) {
304 if (DEBUG) Out.prln(
305 "Failed to remove the listeners for " + res.toString()
306 );
307 throw new
308 ResourceInstantiationException("Parameterisation failure" + e);
309 }
310 }
311 resData.addInstantiation(res);
313 if(features != null) res.getFeatures().putAll(features);
315 creoleProxy.fireResourceLoaded(
317 new CreoleEvent(res, CreoleEvent.RESOURCE_LOADED)
318 );
319 return res;
320 }
322
330 public static void deleteResource(Resource resource) {
331 ResourceData rd =
332 (ResourceData) reg.get(resource.getClass().getName());
333 if(rd!= null)
334 rd.removeInstantiation(resource);
335 creoleProxy.fireResourceUnloaded(
336 new CreoleEvent(resource, CreoleEvent.RESOURCE_UNLOADED)
337 );
338 resource.cleanup();
339 }
341
342 public static Corpus newCorpus(String name)
343 throws ResourceInstantiationException
344 {
345 FeatureMap parameterValues = newFeatureMap();
346 parameterValues.put(Corpus.CORPUS_NAME_PARAMETER_NAME, name);
347 return (Corpus) createResource("gate.corpora.CorpusImpl", parameterValues);
349 }
351
352 public static Document newDocument(URL sourceUrl)
353 throws ResourceInstantiationException
354 {
355 FeatureMap parameterValues = newFeatureMap();
356 parameterValues.put(Document.DOCUMENT_URL_PARAMETER_NAME, sourceUrl);
357 return
358 (Document) createResource("gate.corpora.DocumentImpl", parameterValues);
359 }
361
362 public static Document newDocument(URL sourceUrl, String encoding)
363 throws ResourceInstantiationException
364 {
365 FeatureMap parameterValues = newFeatureMap();
366 parameterValues.put(Document.DOCUMENT_URL_PARAMETER_NAME, sourceUrl);
367 parameterValues.put(Document.DOCUMENT_ENCODING_PARAMETER_NAME, encoding);
368 return
369 (Document) createResource("gate.corpora.DocumentImpl", parameterValues);
370 }
372
373 public static Document newDocument(String content)
374 throws ResourceInstantiationException
375 {
376 FeatureMap params = newFeatureMap();
377 params.put(Document.DOCUMENT_STRING_CONTENT_PARAMETER_NAME, content);
378 Document doc =
379 (Document) createResource("gate.corpora.DocumentImpl", params);
380
385
395 doc.setSourceUrl(null);
396 return doc;
397 }
399
400 public static FeatureMap newFeatureMap() {
401 return new SimpleFeatureMapImpl();
402 }
404
405 public static DataStore openDataStore(
406 String dataStoreClassName, String storageUrl
407 ) throws PersistenceException {
408 DataStore ds = instantiateDataStore(dataStoreClassName, storageUrl);
409 ds.open();
410 if(dsReg.add(ds))
411 creoleProxy.fireDatastoreOpened(
412 new CreoleEvent(ds, CreoleEvent.DATASTORE_OPENED)
413 );
414
415 return ds;
416 }
418
422 public static DataStore createDataStore(
423 String dataStoreClassName, String storageUrl
424 ) throws PersistenceException, UnsupportedOperationException {
425 DataStore ds = instantiateDataStore(dataStoreClassName, storageUrl);
426 ds.create();
427 ds.open();
428 if(dsReg.add(ds))
429 creoleProxy.fireDatastoreCreated(
430 new CreoleEvent(ds, CreoleEvent.DATASTORE_CREATED)
431 );
432
433 return ds;
434 }
436
437 protected static DataStore instantiateDataStore(
438 String dataStoreClassName, String storageUrl
439 ) throws PersistenceException {
440 DataStore godfreyTheDataStore = null;
441 try {
442 godfreyTheDataStore =
443 (DataStore) Gate.getClassLoader().
444 loadClass(dataStoreClassName).newInstance();
445 } catch(Exception e) {
446 throw new PersistenceException("Couldn't create DS class: " + e);
447 }
448
449 if(dsReg == null) dsReg = Gate.getDataStoreRegister();
451 godfreyTheDataStore.setStorageUrl(storageUrl.toString());
452
453 return godfreyTheDataStore;
454 }
456
457 public static synchronized void addCreoleListener(CreoleListener l){
458 creoleProxy.addCreoleListener(l);
459 }
461
462 static {
463 creoleProxy = new CreoleProxy();
464 accessControllerPool = new HashMap();
465 }
467
468
471 public static synchronized AccessController createAccessController(String jdbcURL)
472 throws PersistenceException {
473
474 if (false == accessControllerPool.containsKey(jdbcURL)) {
475 AccessController ac = new AccessControllerImpl(jdbcURL);
476 ac.open();
477 accessControllerPool.put(jdbcURL,ac);
478 }
479
480 return (AccessController)accessControllerPool.get(jdbcURL);
481 }
483 }
485
486
491 class CreoleProxy {
492
493 public synchronized void removeCreoleListener(CreoleListener l) {
494 if (creoleListeners != null && creoleListeners.contains(l)) {
495 Vector v = (Vector) creoleListeners.clone();
496 v.removeElement(l);
497 creoleListeners = v;
498 } }
501 public synchronized void addCreoleListener(CreoleListener l) {
502 Vector v =
503 creoleListeners == null ? new Vector(2) : (Vector) creoleListeners.clone();
504 if (!v.contains(l)) {
505 v.addElement(l);
506 creoleListeners = v;
507 } }
510 protected void fireResourceLoaded(CreoleEvent e) {
511 if (creoleListeners != null) {
512 Vector listeners = creoleListeners;
513 int count = listeners.size();
514 for (int i = 0; i < count; i++) {
515 ((CreoleListener) listeners.elementAt(i)).resourceLoaded(e);
516 } } }
520 protected void fireResourceUnloaded(CreoleEvent e) {
521 if (creoleListeners != null) {
522 Vector listeners = creoleListeners;
523 int count = listeners.size();
524 for (int i = 0; i < count; i++) {
525 ((CreoleListener) listeners.elementAt(i)).resourceUnloaded(e);
526 } } }
530 protected void fireDatastoreOpened(CreoleEvent e) {
531 if (creoleListeners != null) {
532 Vector listeners = creoleListeners;
533 int count = listeners.size();
534 for (int i = 0; i < count; i++) {
535 ((CreoleListener) listeners.elementAt(i)).datastoreOpened(e);
536 } } }
540 protected void fireDatastoreCreated(CreoleEvent e) {
541 if (creoleListeners != null) {
542 Vector listeners = creoleListeners;
543 int count = listeners.size();
544 for (int i = 0; i < count; i++) {
545 ((CreoleListener) listeners.elementAt(i)).datastoreCreated(e);
546 } } }
550 protected void fireDatastoreClosed(CreoleEvent e) {
551 if (creoleListeners != null) {
552 Vector listeners = creoleListeners;
553 int count = listeners.size();
554 for (int i = 0; i < count; i++) {
555 ((CreoleListener) listeners.elementAt(i)).datastoreClosed(e);
556 } } }
560 private transient Vector creoleListeners;
561 }