001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Code is Sun Microsystems, Inc. Portions Copyright 2004-2005 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.jmx.configwizard.generator;
042:
043: import java.text.MessageFormat;
044: import java.io.File;
045: import java.util.Date;
046: import java.util.ArrayList;
047: import java.util.ResourceBundle;
048: import java.io.InputStream;
049:
050: import org.openide.WizardDescriptor;
051: import org.openide.filesystems.FileObject;
052: import org.netbeans.spi.project.ui.templates.support.Templates;
053: import org.openide.loaders.TemplateWizard;
054: import org.openide.util.NbBundle;
055: import org.openide.filesystems.FileUtil;
056: import org.netbeans.modules.jmx.WizardConstants;
057: import org.netbeans.modules.jmx.WizardHelpers;
058: import org.netbeans.modules.jmx.configwizard.JMXConfigWizardIterator;
059: import org.netbeans.modules.jmx.configwizard.RMIAuthenticatedUser;
060:
061: import org.netbeans.modules.jmx.runtime.ManagementDialogs;
062: import org.openide.NotifyDescriptor;
063:
064: /**
065: *
066: * Wizard Management Configuration files generator class
067: */
068: public class ConfigGenerator {
069: // string which represents the date and time of the creation
070: private final String CURRENT_DATE = new Date().toString();
071: private final static String COMMENT = new String("# ");// NOI18N
072: private static final String FALSE = new String("false");// NOI18N
073: private static final String TRUE = new String("true");// NOI18N
074: private static final String FILEPATH = new String("filepath");// NOI18N
075: private static final String INET = new String("<InetAddress>");// NOI18N
076: private static final String CYPHER_SUITES = new String(
077: "<cypher-suites>");// NOI18N
078: private static final String PROTOCOL_VER = new String(
079: "<protocol-versions>");// NOI18N
080: private static final String CONFIG_NAME = new String(
081: "<config-name>");// NOI18N
082: private static final String PORT_NUMBER = new String(
083: "<port-number>");// NOI18N
084: private static final String TRAP_PORT_NUMBER = new String(
085: "<trap-destination-port-number>");// NOI18N
086:
087: private String rmiAccessFile;
088: private String rmiPasswordFile;
089: private boolean rmiSelected;
090: private boolean rmiAuthenticate;
091: private ArrayList rmiAuthenticatedUsers;
092: private boolean rmiPortSelected;
093: private int rmiPort;
094: private boolean sslSelected;
095: private boolean sslReqClientAuth;
096: private boolean sslProtocolsSelected;
097: private String sslProtocols;
098: private boolean sslTlsCipherSelected;
099: private String sslTlsCipher;
100: private boolean snmpSelected;
101: private boolean snmpPortSelected;
102: private int snmpPort;
103: private boolean snmpTrapPortSelected;
104: private int snmpTrapPort;
105: private boolean snmpInterfacesSelected;
106: private String snmpInterfaces;
107: private boolean snmpAclSelected;
108: private boolean snmpAclFileSelected;
109: private String snmpAclFile;
110: private boolean threadMonitor;
111:
112: private void findInfo(TemplateWizard wiz) {
113: Boolean rmiSelectedFinded = ((Boolean) wiz
114: .getProperty(WizardConstants.RMI_SELECTED))
115: .booleanValue();
116: if (rmiSelectedFinded == null) {
117: rmiSelected = false;
118: } else {
119: rmiSelected = rmiSelectedFinded.booleanValue();
120: }
121: rmiAuthenticate = (Boolean) wiz
122: .getProperty(WizardConstants.RMI_AUTHENTICATE);
123:
124: rmiAuthenticatedUsers = (ArrayList) wiz
125: .getProperty(WizardConstants.RMI_AUTHENTICATED_USERS);
126:
127: Integer rmiPortFinded = (Integer) wiz
128: .getProperty(WizardConstants.RMI_PORT);
129: if (rmiPortFinded == null) {
130: rmiPortSelected = false;
131: rmiPort = 0;
132: } else {
133: rmiPortSelected = true;
134: rmiPort = rmiPortFinded;
135: }
136: sslSelected = (Boolean) wiz
137: .getProperty(WizardConstants.SSL_SELECTED);
138: sslReqClientAuth = (Boolean) wiz
139: .getProperty(WizardConstants.RMI_SSL_CLIENT_AUTHENTICATE);
140: String sslProtocolsFinded = (String) wiz
141: .getProperty(WizardConstants.RMI_SSL_PROTOCOLS);
142: if ((sslProtocolsFinded == null)
143: || (sslProtocolsFinded.equals(""))) {// NOI18N
144: sslProtocolsSelected = false;
145: sslProtocols = "";// NOI18N
146: } else {
147: sslProtocolsSelected = true;
148: sslProtocols = sslProtocolsFinded;
149: }
150: String sslTlsCipherFinded = (String) wiz
151: .getProperty(WizardConstants.RMI_SSL_TLS_CIPHER);
152: if ((sslTlsCipherFinded == null)
153: || (sslTlsCipherFinded.equals(""))) {// NOI18N
154: sslTlsCipherSelected = false;
155: sslTlsCipher = "";// NOI18N
156: } else {
157: sslTlsCipherSelected = true;
158: sslTlsCipher = sslTlsCipherFinded;
159: }
160: snmpSelected = (Boolean) wiz
161: .getProperty(WizardConstants.SNMP_SELECTED);
162: Integer snmpPortFinded = (Integer) wiz
163: .getProperty(WizardConstants.SNMP_PORT);
164: if (snmpPortFinded == null) {
165: snmpPortSelected = false;
166: snmpPort = 0;
167: } else {
168: snmpPortSelected = true;
169: snmpPort = snmpPortFinded;
170: }
171: Integer snmpTrapPortFinded = (Integer) wiz
172: .getProperty(WizardConstants.SNMP_TRAP_PORT);
173: if (snmpTrapPortFinded == null) {
174: snmpTrapPortSelected = false;
175: snmpTrapPort = 0;
176: } else {
177: snmpTrapPortSelected = true;
178: snmpTrapPort = snmpTrapPortFinded;
179: }
180: String snmpInterfacesFinded = (String) wiz
181: .getProperty(WizardConstants.SNMP_INTERFACES);
182: if ((snmpInterfacesFinded == null)
183: || (snmpInterfacesFinded.equals(""))) {// NOI18N
184: snmpInterfacesSelected = false;
185: snmpInterfaces = "";// NOI18N
186: } else {
187: snmpInterfacesSelected = true;
188: snmpInterfaces = snmpInterfacesFinded;
189: }
190: snmpAclSelected = (Boolean) wiz
191: .getProperty(WizardConstants.SNMP_ACL);
192: String snmpAclFileFinded = (String) wiz
193: .getProperty(WizardConstants.SNMP_ACL_FILE);
194: if ((snmpAclFileFinded == null)
195: || (snmpAclFileFinded.equals(""))) {// NOI18N
196: snmpAclFileSelected = false;
197: snmpAclFile = "";// NOI18N
198: } else {
199: snmpAclFileSelected = true;
200: snmpAclFile = snmpAclFileFinded;
201: }
202: threadMonitor = (Boolean) wiz
203: .getProperty(WizardConstants.THREAD_CONTENTION_MONITOR);
204: Integer otherPropNbFinded = (Integer) wiz
205: .getProperty(WizardConstants.OTHER_PROP_NUMBER);
206: if (otherPropNbFinded == null) {
207: otherPropNbFinded = 0;
208: }
209: }
210:
211: // returns the management template
212: private String getMgtTemplate(TemplateWizard wiz) throws Exception {
213: FileObject mgtTemplate = Templates.getTemplate(wiz);
214: // keep the pattern
215: InputStream is = mgtTemplate.getInputStream();
216: StringBuffer configContent = new StringBuffer();
217: int ch;
218: while ((ch = is.read()) != -1)
219: configContent.append((char) ch);
220: is.close();
221: return configContent.toString();
222: }
223:
224: // returns the access file template
225: private String getAccessTemplate(TemplateWizard wiz)
226: throws Exception {
227: ResourceBundle bundle = NbBundle
228: .getBundle(JMXConfigWizardIterator.class);
229: FileObject mgtTemplate = Templates.getTemplate(wiz);
230: InputStream is = ((java.net.URL) mgtTemplate
231: .getAttribute("accessTemplate")).openStream(); // NOI18N
232: StringBuffer accessContent = new StringBuffer();
233: int ch;
234: while ((ch = is.read()) != -1)
235: accessContent.append((char) ch);
236:
237: if (rmiAuthenticate) {
238: if (rmiAuthenticatedUsers != null) {
239: for (int i = 0; i < rmiAuthenticatedUsers.size(); i++) {
240: RMIAuthenticatedUser r = (RMIAuthenticatedUser) rmiAuthenticatedUsers
241: .get(i);
242: accessContent
243: .append((r.getName() == null ? "" : r
244: .getName())
245: + " " + // NOI18N
246: (r.getAccess() == null ? "" : r
247: .getAccess()) + "\n");// NOI18N
248: }
249: }
250: }
251:
252: is.close();
253: return accessContent.toString();
254: }
255:
256: // returns the password file template
257: private String getPasswordTemplate(TemplateWizard wiz)
258: throws Exception {
259: FileObject mgtTemplate = Templates.getTemplate(wiz);
260: InputStream is = ((java.net.URL) mgtTemplate
261: .getAttribute("passwordTemplate")).openStream(); // NOI18N
262: StringBuffer passwordContent = new StringBuffer();
263: int ch;
264: while ((ch = is.read()) != -1)
265: passwordContent.append((char) ch);
266:
267: if (rmiAuthenticate) {
268: if (rmiAuthenticatedUsers != null) {
269: for (int i = 0; i < rmiAuthenticatedUsers.size(); i++) {
270: RMIAuthenticatedUser r = (RMIAuthenticatedUser) rmiAuthenticatedUsers
271: .get(i);
272: passwordContent.append((r.getName() == null ? ""
273: : r.getName())
274: + " " + // NOI18N
275: (r.getPassword() == null ? "" : r
276: .getPassword()) + "\n");// NOI18N
277: }
278: }
279: String filePath = (String) wiz
280: .getProperty(WizardConstants.PROP_CONFIG_FILE_PATH);
281: String rmiPasswordFileName = filePath + File.separator
282: + wiz.getTargetName() + "."
283: + WizardConstants.PASSWORD_EXT;// NOI18N
284: ManagementDialogs.getDefault().notify(
285: new NotifyDescriptor.Message(NbBundle
286: .getMessage(ConfigGenerator.class,
287: "MSG_RestrictAccess")
288: + "\n" + rmiPasswordFileName,
289: NotifyDescriptor.INFORMATION_MESSAGE)); // NOI18N
290:
291: }
292:
293: is.close();
294: return passwordContent.toString();
295: }
296:
297: // fill the management properties file
298: private String fillMgtFile(TemplateWizard wiz) throws Exception {
299: MessageFormat formMgtProp = new MessageFormat(
300: getMgtTemplate(wiz));
301: Object[] optionsArgs = new String[31];
302: optionsArgs[0] = Templates.getTargetName(wiz);
303: optionsArgs[1] = CURRENT_DATE;
304: if (rmiSelected && rmiPortSelected) {
305: optionsArgs[2] = "";// NOI18N
306: optionsArgs[3] = String.valueOf(rmiPort);
307: } else {
308: optionsArgs[2] = COMMENT;
309: optionsArgs[3] = PORT_NUMBER;
310: }
311: if (snmpSelected && snmpPortSelected) {
312: optionsArgs[4] = "";// NOI18N
313: optionsArgs[5] = String.valueOf(snmpPort);
314: } else {
315: optionsArgs[4] = COMMENT;
316: optionsArgs[5] = PORT_NUMBER;
317: }
318: if (threadMonitor) {
319: optionsArgs[6] = "";// NOI18N
320: } else {
321: optionsArgs[6] = COMMENT;
322: }
323: if (snmpSelected && snmpTrapPortSelected) {
324: optionsArgs[7] = "";// NOI18N
325: optionsArgs[8] = String.valueOf(snmpTrapPort);
326: } else {
327: optionsArgs[7] = COMMENT;
328: optionsArgs[8] = TRAP_PORT_NUMBER;
329: }
330: if (snmpSelected && snmpInterfacesSelected) {
331: optionsArgs[9] = "";// NOI18N
332: optionsArgs[10] = snmpInterfaces;
333: } else {
334: optionsArgs[9] = COMMENT;
335: optionsArgs[10] = INET;
336: }
337: if (snmpSelected) {
338: optionsArgs[11] = "";// NOI18N
339: if (snmpAclSelected) {
340: optionsArgs[12] = TRUE;
341: } else {
342: optionsArgs[12] = FALSE;
343: }
344: } else {
345: optionsArgs[11] = COMMENT;
346: optionsArgs[12] = FALSE;
347: }
348: if (snmpSelected && snmpAclSelected && snmpAclFileSelected) {
349: optionsArgs[13] = "";// NOI18N
350: optionsArgs[14] = snmpAclFile;
351: } else {
352: optionsArgs[13] = COMMENT;
353: optionsArgs[14] = FILEPATH;
354: }
355: if (rmiSelected) {
356: optionsArgs[15] = "";// NOI18N
357: if (sslSelected) {
358: optionsArgs[16] = TRUE;
359: } else {
360: optionsArgs[16] = FALSE;
361: }
362: } else {
363: optionsArgs[15] = COMMENT;
364: optionsArgs[16] = FALSE;
365: }
366: if (rmiSelected && sslSelected && sslTlsCipherSelected) {
367: optionsArgs[17] = "";// NOI18N
368: optionsArgs[18] = sslTlsCipher;
369: } else {
370: optionsArgs[17] = COMMENT;
371: optionsArgs[18] = CYPHER_SUITES;
372: }
373: if (rmiSelected && sslSelected && sslProtocolsSelected) {
374: optionsArgs[19] = "";// NOI18N
375: optionsArgs[20] = sslProtocols;
376: } else {
377: optionsArgs[19] = COMMENT;
378: optionsArgs[20] = PROTOCOL_VER;
379: }
380: if (rmiSelected && sslSelected) {
381: optionsArgs[21] = "";// NOI18N
382: if (sslReqClientAuth) {
383: optionsArgs[22] = TRUE;
384: } else {
385: optionsArgs[22] = FALSE;
386: }
387: } else {
388: optionsArgs[21] = COMMENT;
389: optionsArgs[22] = TRUE;
390: }
391: if (rmiSelected) {
392: optionsArgs[23] = "";// NOI18N
393: if (rmiAuthenticate) {
394: optionsArgs[24] = TRUE;
395: } else {
396: optionsArgs[24] = FALSE;
397: }
398: } else {
399: optionsArgs[23] = COMMENT;
400: optionsArgs[24] = FALSE;
401: }
402: optionsArgs[25] = COMMENT;
403: optionsArgs[26] = CONFIG_NAME;
404: optionsArgs[27] = "";// NOI18N
405: optionsArgs[28] = FileUtil.toFile(
406: Templates.getTargetFolder(wiz)).getAbsolutePath()
407: .replace(File.separatorChar, '/')
408: + '/'
409: + Templates.getTargetName(wiz)
410: + "."
411: + WizardConstants.PASSWORD_EXT;// NOI18N
412: optionsArgs[29] = "";// NOI18N
413: optionsArgs[30] = FileUtil.toFile(
414: Templates.getTargetFolder(wiz)).getAbsolutePath()
415: .replace(File.separatorChar, '/')
416: + '/'
417: + Templates.getTargetName(wiz)
418: + "."
419: + WizardConstants.ACCESS_EXT;// NOI18N
420: return formMgtProp.format(optionsArgs);
421: }
422:
423: /**
424: * Generate the management configuration files.
425: * It is the entry point to generate Management config files.
426: * @param wiz <CODE>WizardDescriptor</CODE> a wizard
427: * @throws java.io.IOException <CODE>IOException</CODE>
428: * @throws java.lang.Exception <CODE>Exception</CODE>
429: * @return <CODE>FileObject</CODE> the generated management properties file
430: */
431: public FileObject generateConfig(TemplateWizard wiz)
432: throws java.io.IOException, Exception {
433: findInfo(wiz);
434: // add options tag
435: String mgtContent = fillMgtFile(wiz);
436: String accessContent = getAccessTemplate(wiz);
437: String passwordContent = getPasswordTemplate(wiz);
438: return createGeneratedFiles(wiz, mgtContent, accessContent,
439: passwordContent);
440: }
441:
442: /**
443: * Create the Management Configuration files.
444: * @return <CODE>FileObject</CODE> the generated management properties file
445: * @param mgtContent <CODE>String</CODE> expected management properties file content
446: * @param accessContent <CODE>String</CODE> expected access file content
447: * @param passwordContent <CODE>String</CODE> expected password file content
448: * @param wiz <CODE>WizardDescriptor</CODE> a wizard
449: * @throws java.io.IOException <CODE>IOException</CODE>
450: */
451: protected FileObject createGeneratedFiles(WizardDescriptor wiz,
452: String mgtContent, String accessContent,
453: String passwordContent) throws java.io.IOException {
454: // create the access configuration file
455: FileObject accessFile = WizardHelpers.createFile(Templates
456: .getTargetName(wiz), Templates.getTargetFolder(wiz),
457: WizardConstants.ACCESS_EXT, accessContent);
458:
459: // create the password configuration file
460: FileObject passwordFile = WizardHelpers.createFile(Templates
461: .getTargetName(wiz), Templates.getTargetFolder(wiz),
462: WizardConstants.PASSWORD_EXT, passwordContent);
463:
464: // create the mbean implementation file
465: return WizardHelpers.createFile(Templates.getTargetName(wiz),
466: Templates.getTargetFolder(wiz),
467: WizardConstants.PROPERTIES_EXT, mgtContent);
468: }
469:
470: }
|