View Javadoc

1   /*
2    * BaseHandler.java
3    *
4    * Created on April 16, 2004, 1:18 PM
5    */
6   
7   package gov.noaa.gdsg.xmldbremote.service;
8   
9   import java.util.Map;
10  import java.util.HashMap;
11  import gov.noaa.gdsg.xmldbremote.service.transport.BaseTransport;
12  import org.xmldb.api.base.XMLDBException;
13  
14  import org.apache.log4j.Logger;
15  
16  /***
17   * The superclass for handler objects.
18   * @author tns
19   * @version $Id: BaseHandler.java,v 1.2 2004/11/04 00:06:00 mrxtravis Exp $
20   */
21  public abstract class BaseHandler {
22      
23      private static Logger log = Logger.getLogger(BaseHandler.class);
24      
25      //the following are two maps that store references to the same
26      //Collection objects
27      private Map idMap = new HashMap();
28      private Long nextId = new Long(Long.MIN_VALUE);
29      private Object nextIdMutex = new Object();
30      
31      
32      /*** Creates a new instance of BaseHandler */
33      public BaseHandler() {
34          log.debug("BaseHandler constructor for:" + this.getClass().getName());
35      }
36      
37      /***Returns the next id
38       *@return the next available ID
39       */
40      protected Long nextId(){
41          synchronized (this.nextIdMutex){
42              Long next = this.nextId;
43              this.nextId = new Long(next.longValue() + 1);
44              return next;
45          }
46      }
47      
48      /***
49       * Using the transport id, finds the object the transport object represents.
50       * @param transport The object used to represent the actual object.
51       * @return The actual obect.
52       */
53      protected final Object getObjectFromSession(BaseTransport transport){
54          Object object = this.idMap.get(new Long(transport.getTransportId()));
55          if (object == null){
56              if (log.isDebugEnabled()){
57                  log.debug("Could not find transport object in map using id:" + new Long(transport.getTransportId()));
58                  log.debug("The id map has " + this.idMap.size() + " entries");
59                  java.util.Iterator keys = this.idMap.keySet().iterator();
60                  log.debug("Keys:" );
61                  while (keys.hasNext()){
62                      log.debug(" " + keys.next());
63                  }
64                  java.util.Iterator values = this.idMap.values().iterator();
65                  log.debug("values");
66                  while (values.hasNext()){
67                      log.debug(" " + values.next().toString());
68                  }
69              }
70              throw new NullPointerException("Object in session not found for transport object:" + transport);
71          }
72          return object;
73      }
74      
75      /***
76       * saves a service in session and returns the object that represents it
77       * @param object the object to save in session.
78       * @throws org.xmldb.api.base.XMLDBException All exceptions are rethrown as one.
79       * @return The transport object which represents the object saved in the session.
80       */
81      public final BaseTransport saveForSession(Object object) throws XMLDBException {
82          log.debug("Saving object for session:" + object);
83          Long id = this.nextId();
84          log.debug("Next ID is:" + id);
85          this.idMap.put(id, object);
86          log.debug("Put object in map, the map is now size:" + this.idMap.size());
87          BaseTransport transport = createTransportObject(object);
88          transport.setTransportId(id.longValue());
89          return transport;
90      }
91      
92      /*** Returns all known objects being held in session.
93       *@return Object[]
94       */
95      protected Object[] getAllStoredObjects(){
96          return this.idMap.values().toArray();
97      }
98      
99      /***
100      * Subclass implement this in order to create the appropriate object and
101      * populate it with appropriate values.
102      * @param object The actual object in which the mock object should be created to represent.
103      * @throws org.xmldb.api.base.XMLDBException All exceptions are rethrown as one.
104      * @return The transport object representing the given object.
105      */
106     protected abstract BaseTransport createTransportObject(Object object) throws XMLDBException;
107     
108     
109     
110     
111 }