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: DSPersistence.java 13023 2010-08-25 12:43:45Z johann_p $
13 *
14 */
15 package gate.util.persistence;
16
17 import java.net.MalformedURLException;
18 import java.util.Iterator;
19
20 import gate.*;
21 import gate.creole.ResourceInstantiationException;
22 import gate.persist.PersistenceException;
23 import java.net.URL;
24
25 public class DSPersistence implements Persistence{
26
27
28 /**
29 * Populates this Persistence with the data that needs to be stored from the
30 * original source object.
31 */
32 public void extractDataFromSource(Object source)throws PersistenceException{
33 //check input
34 if(! (source instanceof DataStore)){
35 throw new UnsupportedOperationException(
36 getClass().getName() + " can only be used for " +
37 DataStore.class.getName() +
38 " objects!\n" + source.getClass().getName() +
39 " is not a " + DataStore.class.getName());
40 }
41
42 DataStore ds = (DataStore)source;
43 className = ds.getClass().getName();
44 storageUrlString = ds.getStorageUrl();
45 try {
46 storageUrl = PersistenceManager.getPersistentRepresentation(
47 new URL(storageUrlString));
48 } catch(MalformedURLException e) {
49 // ignore and just stay with storageUrlString
50 }
51 }
52
53 /**
54 * Creates a new object from the data contained. This new object is supposed
55 * to be a copy for the original object used as source for data extraction.
56 */
57 public Object createObject()throws PersistenceException,
58 ResourceInstantiationException{
59 if(storageUrl != null) {
60 storageUrlString =
61 ((URL)PersistenceManager.getTransientRepresentation(storageUrl))
62 .toExternalForm();
63 }
64
65 //check if the same datastore is not already open
66 Iterator dsIter = Gate.getDataStoreRegister().iterator();
67 while(dsIter.hasNext()){
68 DataStore aDS = (DataStore)dsIter.next();
69 if(aDS.getStorageUrl().equals(storageUrlString)) {
70 return aDS;
71 }
72 }
73 //if we got this far, then it's a new datastore that needs opening
74 return Factory.openDataStore(className, storageUrlString);
75 }
76
77 protected String className;
78 protected String storageUrlString;
79 protected Object storageUrl;
80 static final long serialVersionUID = 5952924943977701708L;
81 }
|