KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > kernel > InstallationOptions


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.kernel;
66
67 import java.util.Collections JavaDoc;
68 import java.util.HashMap JavaDoc;
69 import java.util.Map JavaDoc;
70
71 /**
72  * Installation is a combination metadata and installation flags for
73  * a particular component. Typical Usage Inside a Component:
74  * <p/>
75  * Creating an Installation Options:
76  * </p><p>
77  * <code><pre>
78  * InstallationOptions returnOptions = new InstallationOptions();
79  * InstallationMetadata newMetadata = returnOptions.createNewMetadata();
80  * newMetadata.setName("Test");
81  * newMetadata.setDefaultValue(Boolean.TRUE);
82  * newMetadata.setType("java.lang.Boolean");
83  * returnOptions.addMetaData(newMetadata);
84  * return returnOptions;
85  * </code></pre></p>
86  * <p/>
87  * Using Installation Options:
88  * </p><p>
89  * <code><pre>
90  * //In the installation section
91  * Boolean deleteTables = installOptions.getInstallOption("Test");
92  * if (deleteTables.booleanValue()) {
93  * //Do some stuff
94  * }
95  * </code</pre>
96  * </p>
97  *
98  * @author Michael Rimov
99  */

100
101 public class InstallationOptions {
102
103     /**
104      * All the installation values keyed by the metadata
105      */

106     protected Map JavaDoc installValues = new HashMap JavaDoc();
107
108     /**
109      * A map for mapping names to metdata
110      */

111     protected Map JavaDoc nameMetadataMap = new HashMap JavaDoc();
112
113
114     public InstallationOptions() {
115
116     }
117
118     /**
119      * Kind of a factory method. Instantiates a new metadata object
120      *
121      * @return
122      */

123     public InstallationMetadata createNewMetadata(String JavaDoc name, Object JavaDoc defaultValue) {
124         InstallationMetadata metadata = new InstallationMetadata();
125         metadata.setName(name);
126         metadata.setDefaultValue(defaultValue);
127         metadata.setDescription(name);
128         return metadata;
129     }
130
131     /**
132      * Called by the <code>Installable</code> implementation to add a filled out
133      * metadata object to the Install Options
134      *
135      * @param metadata the Created and populated metadata
136      */

137     public void addMetaData(InstallationMetadata metadata) {
138         installValues.put(metadata, null);
139         nameMetadataMap.put(metadata.getName(), metadata);
140     }
141
142     /**
143      * Sets the given install option by name
144      *
145      * @param name the name of the installation object
146      * @param newValue the value to set it to.
147      */

148     public void setInstallOption(String JavaDoc name, Object JavaDoc newValue) {
149         InstallationMetadata metadata = (InstallationMetadata) nameMetadataMap.get(name);
150         if (metadata == null) {
151             throw new IllegalArgumentException JavaDoc("Invalid Install Option Name: " + name);
152         }
153
154         installValues.put(metadata, newValue);
155     }
156
157     /**
158      * Retrieves the installation option keyed by name. If the value was never
159      * set then the system returns the default value as defined by the metadata.
160      *
161      * @param name the name of the install option
162      * @return java.lang.Object
163      */

164     public Object JavaDoc getInstallOption(String JavaDoc name) {
165         InstallationMetadata metadata = (InstallationMetadata) nameMetadataMap.get(name);
166         if (metadata == null) {
167             throw new IllegalArgumentException JavaDoc("Invalid Install Option Name: " + name);
168         }
169
170         Object JavaDoc returnValue = installValues.get(metadata);
171
172         if (returnValue == null) {
173             returnValue = metadata.getDefaultValue();
174         }
175
176         return returnValue;
177     }
178
179     public Map JavaDoc getAllInstallValues() {
180         return Collections.unmodifiableMap(installValues);
181     }
182
183     public java.util.Set JavaDoc getAllInstallNames() {
184         return nameMetadataMap.keySet();
185     }
186
187
188     /**
189      * This class provides metadata about any options available.
190      */

191     public class InstallationMetadata {
192
193         public InstallationMetadata() {
194
195         }
196
197         /**
198          * The default value for this particular setting
199          */

200         private Object JavaDoc defaultValue;
201
202         /**
203          * Description for the setting
204          */

205         private String JavaDoc description;
206
207         /**
208          * Type of object the setting is
209          */

210         private String JavaDoc type = null;
211
212         /**
213          * The name of the metadata
214          */

215         private String JavaDoc name;
216
217         public Object JavaDoc getDefaultValue() {
218             return defaultValue;
219         }
220
221         public void setDefaultValue(Object JavaDoc newValue) {
222             defaultValue = newValue;
223
224             //
225
//Automatically set the type if we haven't got any yet.
226
//
227
if (type == null) {
228                 type = newValue.getClass().getName();
229             }
230         }
231
232         public void setDescription(String JavaDoc newValue) {
233             description = newValue;
234         }
235
236         public String JavaDoc getDescription() {
237             return description;
238         }
239
240         public void setType(String JavaDoc newValue) {
241             type = newValue;
242         }
243
244         public String JavaDoc getType() {
245             return type;
246         }
247
248         public void setName(String JavaDoc newValue) {
249             name = newValue;
250         }
251
252         public String JavaDoc getName() {
253             return name;
254         }
255
256         /**
257          * Override of normal hashcode. Here we generate the hashcode
258          * off of the name only. [Since that's unique]
259          *
260          * @return integer
261          */

262         public int hashCode() {
263             return name.hashCode();
264         }
265     }
266 }
Popular Tags