View Javadoc

1   /* (c) Copyright 2003 Caleigo AB, All rights reserved. 
2    * 
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2.1 of the License, or (at your option) any later version.
7    * 
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   * Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   *  
17   */
18  
19  package org.caleigo.core.meta;
20  
21  import java.util.*;
22  
23  import org.caleigo.core.*;
24  import org.caleigo.toolkit.log.Log;
25  
26  
27  /***
28   *
29   * @author  Mattias Hagstrand
30   * @version 1.0
31   */
32  /* 
33  *
34  * WHEN        WHO               WHY & WHAT
35  * ------------------------------------------------------------------------------
36  * 2002-03-25  Mattias Hagstrand    Creation
37  */
38  class CustomDataSourceDescriptor extends AbstractDataSourceDescriptor
39  {
40      // Class members -----------------------------------------------------------
41      private static HashMap sRegisteredDescriptors = new HashMap();
42      
43      // Static methods ----------------------------------------------------------
44      
45      /*** Returns a CustomDataSourceDescriptor with the given code name if it has
46       * been created, otherwise null.
47       * 
48       * All CustomDataSourceDescriptor that are created are automatically registered.
49       * This mehtod has package scope since it is only used to handle serialization
50       * of custom descriptors.
51       */
52      static CustomDataSourceDescriptor getRegisteredCustomDataSourceDescriptor(String codeName)
53      {
54          return (CustomDataSourceDescriptor) sRegisteredDescriptors.get(codeName);
55      }
56      
57  	// Data members ------------------------------------------------------------
58  	private Hashtable mEntityDescriptorCodeNames = new Hashtable();
59      private Hashtable mEntityDescriptorSourceNames = new Hashtable();
60  	
61  	// Constructors ------------------------------------------------------------
62      public CustomDataSourceDescriptor(String codeName, String sourceName, String displayName, String version, boolean readOnly)
63  	{
64  		super(codeName, sourceName, displayName, version, readOnly, null);
65          sRegisteredDescriptors.put(codeName, this);
66      }
67  	
68  	// Superclass overrides ----------------------------------------------------
69  	public int getEntityDescriptorCount()
70      {
71          return mEntityDescriptorCodeNames.size();
72      }
73      
74      public IEntityDescriptor getEntityDescriptor(int index)
75      {
76          Enumeration entityDescriptors = mEntityDescriptorCodeNames.elements();
77          for (int i = 0; i < index && entityDescriptors.hasMoreElements(); i++)
78              entityDescriptors.nextElement();
79          
80          if (entityDescriptors.hasMoreElements())
81              return (IEntityDescriptor) entityDescriptors.nextElement();
82          else
83              return null;
84      }
85  	
86      public IEntityDescriptor getEntityDescriptor(String codeName)
87      {
88          return (IEntityDescriptor) mEntityDescriptorCodeNames.get(codeName);
89      }
90      
91      protected Object writeReplace() throws java.io.ObjectStreamException
92      {
93          return new Dezerializer(this.getCodeName());
94      }
95      
96      protected void finalize()
97      {
98          sRegisteredDescriptors.remove(this.getCodeName());
99      }
100     
101 	// Action methods ----------------------------------------------------------
102 	protected void addEntityDescriptor(IEntityDescriptor entityDescriptor)
103 	{
104 		mEntityDescriptorCodeNames.put(entityDescriptor.getCodeName(), entityDescriptor);
105         mEntityDescriptorSourceNames.put(entityDescriptor.getSourceName(), entityDescriptor);
106 	}
107     
108     protected IEntityDescriptor getEntityDescriptorBySourceName(String sourceName)
109     {
110         return (IEntityDescriptor) mEntityDescriptorSourceNames.get(sourceName);
111     }
112     
113     // Nested classes ----------------------------------------------------------
114     protected static class Dezerializer implements java.io.Serializable
115     {
116         // Data members --------------------------------------------------------
117         protected String mCodeName;
118         
119         // Constructors --------------------------------------------------------
120         public Dezerializer(String codeName)
121         {
122             mCodeName = codeName;
123         }
124         
125         // Action methods ------------------------------------------------------
126         protected Object readResolve() throws java.io.ObjectStreamException
127         {
128             if (sRegisteredDescriptors.containsKey(mCodeName))
129                 return sRegisteredDescriptors.get(mCodeName);
130             else
131             {
132                 Log.printError(this, "CustomDataSourceDescriptor with code name " + mCodeName + " not found");
133                 throw new java.io.InvalidObjectException("CustomDataSourceDescriptor with code name " + mCodeName + " not found");
134             }
135         }
136     }
137 }