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