View Javadoc
1   /*
2    * BaseRecordManager.java
3    *
4    * Created on October 25, 2004, 11:36 AM
5    */
6   
7   package gov.noaa.gdsg.xmldbremote.manage;
8   
9   import org.xmldb.api.base.Database;
10  import org.xmldb.api.base.Collection;
11  import org.xmldb.api.base.XMLDBException;
12  
13  import org.apache.log4j.Logger;
14  
15  /***
16   * Provides basical functionality for managing records.  Keeps track of the database
17   * and collection object.
18   *
19   * @author  tns
20   * @version $Id: CollectionManager.java,v 1.1 2004/10/29 17:47:13 mrxtravis Exp $
21   */
22  public class CollectionManager {
23      
24      private static Logger log = Logger.getLogger(CollectionManager.class);
25      
26      /***
27       * Holds value of property database.
28       */
29      private Database database;
30  
31      /***
32       * Holds value of property collectionURI.
33       */
34      private String collectionURI = null;
35      
36      /***Holds the Collection */
37      private Collection collection = null;
38      /***the collection mutex */
39      private Object collectionMutex = new Object();
40  
41      /***
42       * Holds value of property userName.
43       */
44      private String userName;
45  
46      /***
47       * Holds value of property password.
48       */
49      private String password;    
50      
51      /*** Creates a new instance of BaseRecordManager */
52      public CollectionManager() {
53      }
54      
55      /***
56       * Getter for property database.
57       * @return Value of property database.
58       */
59      public Database getDatabase() {
60  
61          return this.database;
62      }
63  
64      /***
65       * Setter for property database.
66       * @param database New value of property database.
67       */
68      public void setDatabase(Database database) {
69  
70          this.database = database;
71      }
72  
73      /***
74       * Getter for property collectionURI.
75       * @return Value of property collectionURI.
76       */
77      public String getCollectionURI() {
78  
79          return this.collectionURI;
80      }
81  
82      /***
83       * Setter for property collectionURI.
84       * @param collectionURI New value of property collectionURI.
85       */
86      public void setCollectionURI(String collectionURI) {
87          //cannot reset collection URI
88          if (this.collectionURI != null){
89              throw new IllegalStateException("Can not reset the collectionURI");
90          }
91  
92          this.collectionURI = collectionURI;
93      }
94      
95     /***
96       * Getter for property userName.
97       * @return Value of property userName.
98       */
99      public String getUserName() {
100 
101         return this.userName;
102     }
103 
104     /***
105      * Setter for property userName.
106      * @param userName New value of property userName.
107      */
108     public void setUserName(String userName) {
109 
110         this.userName = userName;
111     }
112 
113     /***
114      * Getter for property password.
115      * @return Value of property password.
116      */
117     public String getPassword() {
118 
119         return this.password;
120     }
121 
122     /***
123      * Setter for property password.
124      * @param password New value of property password.
125      */
126     public void setPassword(String password) {
127 
128         this.password = password;
129     }
130     
131     
132  
133     /*** Retrieves the collection */
134     protected Collection getCollection() throws Exception {
135        //either we have a collection or we have a collectionURI
136         if (collection == null && collectionURI == null){
137             throw new IllegalStateException("Property collectionURI is not defined");
138         }
139         
140         //double locking check
141         if (collection == null){
142             synchronized (collectionMutex){
143                 if (collection == null){
144                     log.info("Requesting Collection from the Database");
145                     collection = this.database.getCollection(this.collectionURI,this.userName,this.password);
146                 }
147             }
148         }
149         return collection;
150     }
151     
152 }