001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)CustomConfig.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package deploytest;
030:
031: import java.io.File;
032: import java.util.Set;
033: import java.util.Vector;
034:
035: import javax.jbi.component.ComponentContext;
036:
037: import javax.management.MBeanException;
038: import javax.management.openmbean.CompositeData;
039: import javax.management.openmbean.CompositeDataSupport;
040: import javax.management.openmbean.CompositeType;
041: import javax.management.openmbean.TabularData;
042: import javax.management.openmbean.TabularDataSupport;
043: import javax.management.openmbean.TabularType;
044: import javax.management.openmbean.OpenType;
045: import javax.management.openmbean.SimpleType;
046: import javax.management.openmbean.ArrayType;
047:
048: public class CustomConfig implements CustomConfigMBean {
049: private String mInfo;
050:
051: private int mPort;
052: private String mHostName;
053: private String mProxyPwd;
054:
055: /** The application variables TabularData */
056: private TabularDataSupport mAppVars;
057:
058: /** Application Variable Composite Type */
059: private static CompositeType sAppVarComposite;
060:
061: /** Application Variable Tabular Type */
062: private static TabularType sAppVarTable;
063:
064: /** Application Variable Composite Item Names */
065: private static String[] sAppVarItemNames;
066:
067: /** The index of the Application Variables **/
068: private static String[] sappVarRowIndex;
069:
070: /** The application configuration TabularData */
071: private TabularDataSupport mAppConfigs;
072:
073: /** Application Configuration Composite Type */
074: private static CompositeType sAppConfigComposite;
075:
076: /** Application Configuration Tabular Type */
077: private static TabularType sAppConfigTable;
078:
079: /** Application Configuration Composite Item Names */
080: private static String[] sAppConfigItemNames;
081:
082: /** The index of the Application Configurations **/
083: private static String[] sAppConfigRowIndex;
084:
085: /** Configuration display data and schema */
086: private static String mConfigXml, mConfigXsd;
087:
088: /** Component Context */
089: private ComponentContext mContext;
090:
091: private void initOpenTypes()
092: throws javax.management.openmbean.OpenDataException {
093: // Define the Application Variables CompositeType
094: sAppVarItemNames = new String[] { "name", "value", "type" };
095:
096: String[] appVarItemDesc = { "Application variable name",
097: "Application variable value",
098: "Application variable type" };
099:
100: OpenType[] appVarRowAttrTypes = { SimpleType.STRING,
101: SimpleType.STRING, SimpleType.STRING };
102:
103: sappVarRowIndex = new String[] { "name" };
104:
105: sAppVarComposite = new CompositeType(
106: "ApplicationVariableComposite",
107: "Application variable name and value pair",
108: sAppVarItemNames, appVarItemDesc, appVarRowAttrTypes);
109:
110: sAppVarTable = new TabularType("ApplicationVariableList",
111: "List of application variables", sAppVarComposite,
112: sappVarRowIndex);
113:
114: // Define the Application Configuration OpenType
115: sAppConfigItemNames = new String[] { "configurationName",
116: "connectionURL", "securityPrincipal",
117: "securityCredential", "jndienv" };
118:
119: String[] appConfigItemDesc = {
120: "Application configuration name", "Connection URL",
121: "Security Principal", "Security Credential",
122: "JNDI Properties" };
123:
124: ArrayType jndiEnvArrayType = new ArrayType(1, SimpleType.STRING);
125: OpenType[] appConfigRowAttrTypes = { SimpleType.STRING,
126: SimpleType.STRING, SimpleType.STRING,
127: SimpleType.STRING, jndiEnvArrayType };
128:
129: sAppConfigRowIndex = new String[] { "configurationName" };
130:
131: sAppConfigComposite = new CompositeType(
132: "ApplicationConfigurationComposite",
133: "Application Configuration Composite",
134: sAppConfigItemNames, appConfigItemDesc,
135: appConfigRowAttrTypes);
136:
137: sAppConfigTable = new TabularType(
138: "ApplicationConfigurationList",
139: "List of application configurations",
140: sAppConfigComposite, sAppConfigRowIndex);
141: }
142:
143: /**
144: * HostName gettter/setter
145: */
146: public void setHostName(String hostName) {
147: mHostName = hostName;
148: }
149:
150: public String getHostName() {
151: return mHostName;
152: }
153:
154: /**
155: * Port get/set
156: */
157: public void setPort(int port) {
158: mPort = port;
159: }
160:
161: public int getPort() {
162: return mPort;
163: }
164:
165: /**
166: * ProxyPassword - secure field
167: */
168: public void setProxyPassword(String proxyPassword) {
169: mProxyPwd = proxyPassword;
170: }
171:
172: public String getProxyPassword() {
173: return mProxyPwd;
174: }
175:
176: public CustomConfig(String info, ComponentContext ctx) {
177: mInfo = info;
178: mContext = ctx;
179: try {
180: initOpenTypes();
181: mAppVars = new TabularDataSupport(sAppVarTable);
182: mAppConfigs = new TabularDataSupport(sAppConfigTable);
183:
184: // Add a default app var
185: CompositeDataSupport cd = new CompositeDataSupport(
186: sAppVarComposite, sAppVarItemNames, new String[] {
187: "default", "defValue", "STRING" });
188: mAppVars.put(cd);
189:
190: // Add a default app config
191: CompositeDataSupport appCfgCD = new CompositeDataSupport(
192: sAppConfigComposite,
193: sAppConfigItemNames,
194: new Object[] {
195: "defaultConfig",
196: "http://www.sun.com",
197: "administrator",
198: "abc",
199: new String[] { "env1=value1", "env2=value2" } });
200: mAppConfigs.put(appCfgCD);
201:
202: // Initialize the component configuration xsd and xml
203: readConfigData();
204: } catch (Exception ex) {
205: System.out
206: .println("Error in populating the Application Variables");
207: System.out.println(ex.getMessage());
208: }
209: }
210:
211: /**
212: * Test method.
213: */
214: public String getComponentCustomInfo() {
215: return mInfo;
216: }
217:
218: /**
219: * Get the CompositeType definition for the components application configuration
220: *
221: * @return the CompositeType for the components application configuration.
222: */
223: public CompositeType queryApplicationConfigurationType() {
224: return sAppConfigComposite;
225: }
226:
227: /**
228: * Add an application configuration. The configuration name is a part of the CompositeData.
229: * The itemName for the configuration name is "configurationName" and the type is SimpleType.STRING
230: *
231: * @param name - configuration name, must match the value of the field "name" in the namedConfig
232: * @param appConfig - application configuration composite
233: * @throws MBeanException if the application configuration cannot be added.
234: * @return management message string which gives the status of the operation. For
235: * target=cluster, instance specific details are included.
236: */
237: public void addApplicationConfiguration(String name,
238: CompositeData appConfig) throws MBeanException {
239: mAppConfigs.put(appConfig);
240: }
241:
242: /**
243: * Delete an application configuration.
244: *
245: * @param name - identification of the application configuration to be deleted
246: * @throws MBeanException if the configuration cannot be deleted.
247: * @return management message string which gives the status of the operation. For
248: * target=cluster, instance specific details are included.
249: */
250: public void deleteApplicationConfiguration(String name)
251: throws MBeanException {
252: CompositeData cd = (CompositeData) mAppConfigs
253: .get(new String[] { name });
254:
255: if (cd != null) {
256: mAppConfigs.remove(new String[] { name });
257: }
258: }
259:
260: /**
261: * Update a application configuration. The configuration name is a part of the CompositeData.
262: * The itemName for the configuration name is "configurationName" and the type is SimpleType.STRING
263: *
264: * @param name - configuration name, must match the value of the field "configurationName" in the appConfig
265: * @param appConfig - application configuration composite
266: * @throws MBeanException if there are errors encountered when updating the configuration.
267: * @return management message string which gives the status of the operation. For
268: * target=cluster, instance specific details are included.
269: */
270: public void setApplicationConfiguration(String name,
271: CompositeData appConfig) throws MBeanException {
272: CompositeData cd = (CompositeData) mAppConfigs
273: .get(new String[] { name });
274:
275: if (cd != null) {
276:
277: CompositeData updatedCd = updateConfig(name, cd, appConfig);
278: mAppConfigs.remove(new String[] { name });
279: mAppConfigs.put(updatedCd);
280: }
281: }
282:
283: /**
284: * Get a Map of all application configurations for the component.
285: *
286: * @return a TabularData of all the application configurations for a
287: * component keyed by the configuration name.
288: */
289: public TabularData getApplicationConfigurations() {
290: return mAppConfigs;
291: }
292:
293: /**
294: * This operation adds a new application variable. If a variable already exists with
295: * the same name as that specified then the operation fails.
296: *
297: * @param name - name of the application variable
298: * @param appVar - this is the application variable compoiste
299: * @throws MBeanException if an error occurs in adding the application variables to the
300: * component.
301: * @return management message string which gives the status of the operation. For
302: * target=cluster, instance specific details are included.
303: */
304: public void addApplicationVariable(String name, CompositeData appVar)
305: throws MBeanException {
306: mAppVars.put(appVar);
307: }
308:
309: /**
310: * This operation sets an application variable. If a variable does not exist with
311: * the same name, its an error.
312: *
313: * @param name - name of the application variable
314: * @param appVar - this is the application variable compoiste to be updated.
315: * @throws MBeanException if one or more application variables cannot be deleted
316: * @return management message string which gives the status of the operation. For
317: * target=cluster, instance specific details are included.
318: */
319: public void setApplicationVariable(String name, CompositeData appVar)
320: throws MBeanException {
321: CompositeData cd = (CompositeData) mAppVars
322: .get(new String[] { name });
323:
324: if (cd != null) {
325: mAppVars.remove(new String[] { name });
326: mAppVars.put(appVar);
327: }
328: }
329:
330: /**
331: * This operation deletes an application variable, if a variable with the specified name does
332: * not exist, it's an error.
333: *
334: * @param name - name of the application variable
335: * @throws MBeanException on errors.
336: * @return management message string which gives the status of the operation. For
337: * target=cluster, instance specific details are included.
338: */
339: public void deleteApplicationVariable(String name)
340: throws MBeanException {
341: mAppVars.remove(new String[] { name });
342: }
343:
344: /**
345: * Get the Application Variable set for a component.
346: *
347: * @return a TabularData which has all the applicationvariables set on the component.
348: */
349: public TabularData getApplicationVariables() {
350: return mAppVars;
351: }
352:
353: /**
354: * Retrieves the component specific configuration schema.
355: *
356: * @return a String containing the configuration schema.
357: */
358: public String retrieveConfigurationDisplaySchema() {
359: return mConfigXsd;
360: }
361:
362: /**
363: * Retrieves the component configuration metadata.
364: * The XML data conforms to the component
365: * configuration schema.
366: *
367: * @return a String containing the configuration metadata.
368: */
369: public String retrieveConfigurationDisplayData() {
370: return mConfigXml;
371: }
372:
373: /**
374: * Read the configuration xsd/xml based on the old schema
375: */
376: private void readConfigData() throws Exception {
377: String installRoot = mContext.getInstallRoot();
378:
379: File componentConfigData = new File(installRoot,
380: "/META-INF/componentConfiguration.xml");
381:
382: if (componentConfigData.exists()) {
383: mConfigXml = readFile(componentConfigData);
384: }
385:
386: File componentConfigSchema = new File(installRoot,
387: "/META-INF/componentConfiguration.xsd");
388:
389: if (componentConfigSchema.exists()) {
390: mConfigXsd = readFile(componentConfigSchema);
391: }
392: }
393:
394: /**
395: * @return the contents of the file as a String
396: */
397: private String readFile(File aFile) throws Exception {
398: String result = "";
399:
400: if (aFile.exists()) {
401: java.io.FileInputStream fis = new java.io.FileInputStream(
402: aFile);
403:
404: byte[] bigBuffer = new byte[(int) aFile.length()];
405:
406: fis.read(bigBuffer);
407: fis.close();
408:
409: result = new String(bigBuffer);
410: }
411:
412: return result;
413: }
414:
415: /**
416: * Merge two application configurations for update
417: *
418: * @param oldConfig
419: * @param newConfig
420: * @return the updated CompositeData
421: */
422: private CompositeData updateConfig(String name,
423: CompositeData oldConfig, CompositeData newConfig) {
424: Set<String> keySet = sAppConfigComposite.keySet();
425:
426: Vector<String> keys = new Vector();
427: Vector<Object> values = new Vector();
428:
429: for (String key : keySet) {
430: if (key.equals("configurationName")) {
431: keys.add(key);
432: values.add(name);
433: continue;
434: }
435:
436: if (newConfig.containsKey(key)
437: && newConfig.get(key) != null) {
438: keys.add(key);
439: values.add(newConfig.get(key));
440: } else if (oldConfig.containsKey(key)) {
441: keys.add(key);
442: values.add(oldConfig.get(key));
443: }
444:
445: }
446:
447: CompositeData cd = null;
448: try {
449: cd = new CompositeDataSupport(sAppConfigComposite, keys
450: .toArray(new String[keys.size()]), values
451: .toArray(new Object[values.size()]));
452: } catch (Exception ex) {
453: ex.printStackTrace();
454: }
455:
456: return cd;
457: }
458: }
|