PRPersistence.java
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 26/10/2001
11  *
12  *  $Id: PRPersistence.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 
23 
24 public class PRPersistence extends ResourcePersistence {
25   /**
26    * Populates this Persistence with the data that needs to be stored from the
27    * original source object.
28    */
29   public void extractDataFromSource(Object source)throws PersistenceException{
30     if((source instanceof ProcessingResource)){
31       throw new UnsupportedOperationException(
32                 getClass().getName() " can only be used for " +
33                 ProcessingResource.class.getName() +
34                 " objects!\n" + source.getClass().getName() +
35                 " is not a " + ProcessingResource.class.getName());
36     }
37 
38     super.extractDataFromSource(source);
39     Resource res = (Resource)source;
40 
41     ResourceData rData = (ResourceData)
42                          Gate.getCreoleRegister().get(res.getClass().getName());
43     if(rData == nullthrow new PersistenceException(
44                                 "Could not find CREOLE data for " +
45                                 res.getClass().getName());
46 
47     //now get the runtime params
48     ParameterList params = rData.getParameterList();
49     try{
50       //get the values for the init time parameters
51       runtimeParams = Factory.newFeatureMap();
52       //this is a list of lists
53       Iterator parDisjIter = ((List)params.getRuntimeParameters()).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)runtimeParams).put(parName,parValue);
61         }
62       }
63       runtimeParams = PersistenceManager.
64                       getPersistentRepresentation(runtimeParams);
65     }catch(ResourceInstantiationException rie){
66       throw new PersistenceException(rie);
67     }
68   }
69 
70   /**
71    * Creates a new object from the data contained. This new object is supposed
72    * to be a copy for the original object used as source for data extraction.
73    */
74   public Object createObject()throws PersistenceException,
75                                      ResourceInstantiationException{
76     Object res = super.createObject();
77     //now add the runtime parameters
78     if(runtimeParams != null){
79       runtimeParams = PersistenceManager.
80                       getTransientRepresentation(runtimeParams);
81       ((Resource)res).setParameterValues((FeatureMap)runtimeParams);
82     }
83 
84     return res;
85   }
86 
87   protected Object runtimeParams;
88   static final long serialVersionUID = 2031381604712340552L;
89 }