01 /*
02 * Copyright (c) 1995-2010, The University of Sheffield. See the file
03 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
04 *
05 * This file is part of GATE (see http://gate.ac.uk/), and is free
06 * software, licenced under the GNU Library General Public License,
07 * Version 2, June 1991 (in the distribution as file licence.html,
08 * and also available at http://gate.ac.uk/gate/licence.html).
09 *
10 * Valentin Tablan 25/10/2001
11 *
12 * $Id: ResourcePersistence.java 12006 2009-12-01 17:24:28Z thomas_heitz $
13 *
14 */
15 package gate.util.persistence;
16
17 import java.util.*;
18
19 import gate.*;
20 import gate.creole.*;
21 import gate.persist.PersistenceException;
22 import gate.util.NameBearer;
23
24 /**
25 * Holds the data needed to serialise and recreate a {@link Resource}.
26 * This data is considered to be: the resource class name, the resource name,
27 * the resource features and the resource initialistion parameters.
28 */
29 class ResourcePersistence implements Persistence{
30
31 public void extractDataFromSource(Object source) throws PersistenceException{
32 if(! (source instanceof Resource)){
33 throw new UnsupportedOperationException(
34 getClass().getName() + " can only be used for " +
35 Resource.class.getName() +
36 " objects!\n" + source.getClass().getName() +
37 " is not a " + Resource.class.getName());
38 }
39 Resource res = (Resource)source;
40 resourceType = res.getClass().getName();
41 if(res instanceof NameBearer) resourceName = ((NameBearer)res).getName();
42
43 ResourceData rData = (ResourceData)
44 Gate.getCreoleRegister().get(resourceType);
45 if(rData == null) throw new PersistenceException(
46 "Could not find CREOLE data for " +
47 resourceType);
48 ParameterList params = rData.getParameterList();
49 try{
50 //get the values for the init time parameters
51 initParams = Factory.newFeatureMap();
52 //this is a list of lists
53 Iterator parDisjIter = ((List)params.getInitimeParameters()).iterator();
54 while(parDisjIter.hasNext()){
55 Iterator parIter = ((List)parDisjIter.next()).iterator();
56 while(parIter.hasNext()){
57 Parameter parameter = (Parameter)parIter.next();
58 String parName = parameter.getName();
59 Object parValue = res.getParameterValue(parName);
60 ((Map)initParams).put(parName, parValue);
61 }
62 }
63 initParams = PersistenceManager.getPersistentRepresentation(initParams);
64
65 //get the features
66 if(res.getFeatures() != null){
67 features = Factory.newFeatureMap();
68 ((Map)features).putAll(res.getFeatures());
69 features = PersistenceManager.getPersistentRepresentation(features);
70 }
71 }catch(ResourceInstantiationException rie){
72 throw new PersistenceException(rie);
73 }
74 }
75
76
77 public Object createObject()throws PersistenceException,
78 ResourceInstantiationException {
79 if(initParams != null)
80 initParams = PersistenceManager.getTransientRepresentation(initParams);
81 if(features != null)
82 features = PersistenceManager.getTransientRepresentation(features);
83 Resource res = Factory.createResource(resourceType, (FeatureMap)initParams,
84 (FeatureMap)features,resourceName);
85 return res;
86 }
87
88 protected String resourceType;
89 protected String resourceName;
90 protected Object initParams;
91 protected Object features;
92 static final long serialVersionUID = -3196664486112887875L;
93 }
|