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 java4ant;
030:
031: import java.io.File;
032:
033: import javax.management.MBeanException;
034: import javax.management.openmbean.ArrayType;
035: import javax.management.openmbean.CompositeData;
036: import javax.management.openmbean.CompositeDataSupport;
037: import javax.management.openmbean.CompositeType;
038: import javax.management.openmbean.TabularData;
039: import javax.management.openmbean.TabularDataSupport;
040: import javax.management.openmbean.TabularType;
041: import javax.management.openmbean.OpenType;
042: import javax.management.openmbean.SimpleType;
043:
044: import javax.jbi.component.ComponentContext;
045:
046: public class CustomConfig implements CustomConfigMBean {
047: private String mInfo;
048: private String mA, mB, mC;
049: /** The application variables TabularData */
050: private TabularDataSupport mAppVars;
051: private ComponentContext mContext;
052:
053: /** Application Variable Composite Type */
054: private static CompositeType sAppVarComposite;
055:
056: /** Application Variable Tabular Type */
057: private static TabularType sAppVarTable;
058:
059: /** Application Variable Composite Item Names */
060: private static String[] sAppVarItemNames;
061:
062: /** The index of the Application Variables **/
063: private static String[] sappVarRowIndex;
064:
065: /** The application configuration TabularData */
066: private TabularDataSupport mAppConfigs;
067:
068: /** Application Configuration Composite Type */
069: private static CompositeType sAppConfigComposite;
070:
071: /** Application Configuration Tabular Type */
072: private static TabularType sAppConfigTable;
073:
074: /** Application Configuration Composite Item Names */
075: private static String[] sAppConfigItemNames;
076:
077: /** The index of the Application Configurations **/
078: private static String[] sAppConfigRowIndex;
079:
080: /** NOTE : If you update this XSD document the admincommon00009 test would
081: * need to be updated too
082: */
083: private static String CONFIG_XSD;
084:
085: /** NOTE : If you update this XML document the admincommon00009 test would
086: * need to be updated too
087: */
088: private static String CONFIG_XML;
089:
090: private void initOpenTypes()
091: throws javax.management.openmbean.OpenDataException {
092: // Define the Application Variables CompositeType
093: sAppVarItemNames = new String[] { "name", "value", "type" };
094:
095: String[] appVarItemDesc = { "Application variable name",
096: "Application variable value",
097: "Application variable type" };
098:
099: OpenType[] appVarRowAttrTypes = { SimpleType.STRING,
100: SimpleType.STRING, SimpleType.STRING };
101:
102: sappVarRowIndex = new String[] { "name" };
103:
104: sAppVarComposite = new CompositeType(
105: "ApplicationVariableComposite",
106: "Application variable name and value pair",
107: sAppVarItemNames, appVarItemDesc, appVarRowAttrTypes);
108:
109: sAppVarTable = new TabularType("ApplicationVariableList",
110: "List of application variables", sAppVarComposite,
111: sappVarRowIndex);
112:
113: // Define the Application Configuration OpenType
114: sAppConfigItemNames = new String[] { "configurationName",
115: "connectionURL", "securityPrincipal",
116: "securityCredential", "jndienv" };
117:
118: String[] appConfigItemDesc = {
119: "Application configuration name", "Connection URL",
120: "Security Principal", "Security Credential",
121: "JNDI Properties" };
122:
123: ArrayType jndiEnvArrayType = new ArrayType(1, SimpleType.STRING);
124: OpenType[] appConfigRowAttrTypes = { SimpleType.STRING,
125: SimpleType.STRING, SimpleType.STRING,
126: SimpleType.STRING, jndiEnvArrayType };
127:
128: sAppConfigRowIndex = new String[] { "configurationName" };
129:
130: sAppConfigComposite = new CompositeType(
131: "ApplicationConfigurationComposite",
132: "Application Configuration Composite",
133: sAppConfigItemNames, appConfigItemDesc,
134: appConfigRowAttrTypes);
135:
136: sAppConfigTable = new TabularType(
137: "ApplicationConfigurationList",
138: "List of application configurations",
139: sAppConfigComposite, sAppConfigRowIndex);
140: }
141:
142: public CustomConfig(String info, ComponentContext context) {
143: mContext = context;
144: mInfo = info;
145: try {
146: initOpenTypes();
147: mAppVars = new TabularDataSupport(sAppVarTable);
148: mAppConfigs = new TabularDataSupport(sAppConfigTable);
149:
150: // Add a default app var
151: CompositeDataSupport cd = new CompositeDataSupport(
152: sAppVarComposite, sAppVarItemNames, new String[] {
153: "default", "defValue", "STRING" });
154: mAppVars.put(cd);
155:
156: // Add a default app config
157: CompositeDataSupport appCfgCD = new CompositeDataSupport(
158: sAppConfigComposite,
159: sAppConfigItemNames,
160: new Object[] {
161: "defaultConfig",
162: "http://www.sun.com",
163: "administrator",
164: "abc",
165: new String[] { "env1=value1", "env2=value2" } });
166: mAppConfigs.put(appCfgCD);
167:
168: // Initialize the component configuration xsd and xml
169: readConfigData();
170:
171: mA = System.getProperty("A", "");
172: mB = System.getProperty("B", "");
173: } catch (Exception ex) {
174: System.out
175: .println("Error in populating the Application Variables");
176: System.out.println(ex.getMessage());
177: }
178: }
179:
180: /**
181: * Test method.
182: */
183: public String getComponentCustomInfo() {
184: return mInfo;
185: }
186:
187: /**
188: * Attribute getters/setters
189: */
190: public String getA() {
191: return mA;
192: }
193:
194: public void setA(String a) {
195: mA = a;
196: }
197:
198: public String getB() {
199: return mB;
200: }
201:
202: public void setB(String b) {
203: mB = b;
204: }
205:
206: public String getC() {
207: return mC;
208: }
209:
210: public void setC(String c) {
211: mC = c;
212: }
213:
214: /**
215: * This operation adds a new application variable. If a variable already exists with
216: * the same name as that specified then the operation fails.
217: *
218: * @param name - name of the application variable
219: * @param appVar - this is the application variable compoiste
220: * @throws MBeanException if an error occurs in adding the application variables to the
221: * component.
222: * @return management message string which gives the status of the operation. For
223: * target=cluster, instance specific details are included.
224: */
225: public void addApplicationVariable(String name, CompositeData appVar)
226: throws MBeanException {
227: mAppVars.put(appVar);
228: }
229:
230: /**
231: * This operation sets an application variable. If a variable does not exist with
232: * the same name, its an error.
233: *
234: * @param name - name of the application variable
235: * @param appVar - this is the application variable compoiste to be updated.
236: * @throws MBeanException if one or more application variables cannot be deleted
237: * @return management message string which gives the status of the operation. For
238: * target=cluster, instance specific details are included.
239: */
240: public void setApplicationVariable(String name, CompositeData appVar)
241: throws MBeanException {
242: CompositeData cd = (CompositeData) mAppVars
243: .get(new String[] { name });
244:
245: if (cd != null) {
246: mAppVars.remove(new String[] { name });
247: mAppVars.put(appVar);
248: }
249: }
250:
251: /**
252: * This operation deletes an application variable, if a variable with the specified name does
253: * not exist, it's an error.
254: *
255: * @param name - name of the application variable
256: * @throws MBeanException on errors.
257: * @return management message string which gives the status of the operation. For
258: * target=cluster, instance specific details are included.
259: */
260: public void deleteApplicationVariable(String name)
261: throws MBeanException {
262: mAppVars.remove(new String[] { name });
263: }
264:
265: /**
266: * Get the Application Variable set for a component.
267: *
268: * @return a TabularData which has all the applicationvariables set on the component.
269: */
270: public TabularData getApplicationVariables() {
271: return mAppVars;
272: }
273:
274: /**
275: * Get the CompositeType definition for the components application configuration
276: *
277: * @return the CompositeType for the components application configuration.
278: */
279: public CompositeType queryApplicationConfigurationType() {
280: return sAppConfigComposite;
281: }
282:
283: /**
284: * Add an application configuration. The configuration name is a part of the CompositeData.
285: * The itemName for the configuration name is "configurationName" and the type is SimpleType.STRING
286: *
287: * @param name - configuration name, must match the value of the field "name" in the namedConfig
288: * @param appConfig - application configuration composite
289: * @throws MBeanException if the application configuration cannot be added.
290: * @return management message string which gives the status of the operation. For
291: * target=cluster, instance specific details are included.
292: */
293: public void addApplicationConfiguration(String name,
294: CompositeData appConfig) throws MBeanException {
295: mAppConfigs.put(appConfig);
296: }
297:
298: /**
299: * Delete an application configuration.
300: *
301: * @param name - identification of the application configuration to be deleted
302: * @throws MBeanException if the configuration cannot be deleted.
303: * @return management message string which gives the status of the operation. For
304: * target=cluster, instance specific details are included.
305: */
306: public void deleteApplicationConfiguration(String name)
307: throws MBeanException {
308: CompositeData cd = (CompositeData) mAppConfigs
309: .get(new String[] { name });
310:
311: if (cd != null) {
312: mAppConfigs.remove(new String[] { name });
313: }
314: }
315:
316: /**
317: * Update a application configuration. The configuration name is a part of the CompositeData.
318: * The itemName for the configuration name is "configurationName" and the type is SimpleType.STRING
319: *
320: * @param name - configuration name, must match the value of the field "configurationName" in the appConfig
321: * @param appConfig - application configuration composite
322: * @throws MBeanException if there are errors encountered when updating the configuration.
323: * @return management message string which gives the status of the operation. For
324: * target=cluster, instance specific details are included.
325: */
326: public void setApplicationConfiguration(String name,
327: CompositeData appConfig) throws MBeanException {
328: CompositeData cd = (CompositeData) mAppConfigs
329: .get(new String[] { name });
330:
331: if (cd != null) {
332: mAppConfigs.remove(new String[] { name });
333: mAppConfigs.put(appConfig);
334: }
335: }
336:
337: /**
338: * Get a Map of all application configurations for the component.
339: *
340: * @return a TabularData of all the application configurations for a
341: * component keyed by the configuration name.
342: */
343: public TabularData getApplicationConfigurations() {
344: return mAppConfigs;
345: }
346:
347: /**
348: * Retrieves the component specific configuration schema.
349: *
350: * @return a String containing the configuration schema.
351: */
352: public String retrieveConfigurationDisplaySchema() {
353: // Returning an empty schema for testing purposes
354: return CONFIG_XSD;
355: }
356:
357: /**
358: * Retrieves the component configuration metadata.
359: * The XML data conforms to the component
360: * configuration schema.
361: *
362: * @return a String containing the configuration metadata.
363: */
364: public String retrieveConfigurationDisplayData() {
365: return CONFIG_XML;
366: }
367:
368: public String getCONFIG_XML() {
369: return CONFIG_XML;
370: }
371:
372: /**
373: * Read the configuration xsd/xml based on the old schema
374: */
375: private void readConfigData() throws Exception {
376: String installRoot = mContext.getInstallRoot();
377:
378: File componentConfigData = new File(installRoot,
379: "/META-INF/componentConfiguration.xml");
380:
381: if (componentConfigData.exists()) {
382: CONFIG_XML = readFile(componentConfigData);
383: }
384:
385: File componentConfigSchema = new File(installRoot,
386: "/META-INF/componentConfiguration.xsd");
387:
388: if (componentConfigSchema.exists()) {
389: CONFIG_XSD = readFile(componentConfigSchema);
390: }
391: }
392:
393: /**
394: * @return the contents of the file as a String
395: */
396: private String readFile(File aFile) throws Exception {
397: String result = "";
398:
399: if (aFile.exists()) {
400: java.io.FileInputStream fis = new java.io.FileInputStream(
401: aFile);
402:
403: byte[] bigBuffer = new byte[(int) aFile.length()];
404:
405: fis.read(bigBuffer);
406: fis.close();
407:
408: result = new String(bigBuffer);
409: }
410:
411: return result;
412: }
413:
414: }
|