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: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 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.identity.profile.api.configurator;
042:
043: import java.io.FileInputStream;
044: import java.util.Enumeration;
045: import java.util.HashMap;
046: import java.util.Properties;
047: import org.openide.util.NbBundle;
048:
049: /**
050: * Custom Properties class for loading and manipulating the AMConfig.properties
051: * file.
052: *
053: * Created on July 5, 2006, 5:39 PM
054: *
055: * @author ptliu
056: */
057: public class ServerProperties extends Properties implements Cloneable {
058:
059: public static final String PROP_ID = "org.netbeans.modules.identity.profile.api.configurator.id"; //NOI18N
060:
061: public static final String PROP_HOST = "com.iplanet.am.server.host"; //NOI18N
062:
063: public static final String PROP_PORT = "com.iplanet.am.server.port"; //NOI18N
064:
065: public static final String PROP_CONTEXT_ROOT = "org.netbeans.modules.identity.profile.api.configurator.contextroot"; //NOI18N
066:
067: public static final String PROP_USERNAME = "com.sun.identity.agents.app.username"; //NOI18N
068:
069: public static final String PROP_PASSWORD = "com.iplanet.am.service.password"; //NOI18N
070:
071: private static final String PROP_PROTOCOL = "com.iplanet.am.server.protocol"; //NOI18N
072:
073: public static final String PROP_AM_CONFIG_FILE = "org.netbeans.modules.identity.profile.api.configurator.amconfigfile"; //NOI18N
074:
075: public static final String PROP_AM_CONSOLE_URL = "org.netbeans.modules.identity.profile.api.configurator.amconsoleurl"; //NOI18N
076:
077: private static final String PROP_NAMING_SERVICE_URL = "com.iplanet.am.naming.url"; //NOI18N
078:
079: public static final String PROP_IS_ALIVE_URL = "org.netbeans.modules.identity.profile.api.configurator.isaliveurl"; //NOI18N
080:
081: public static final String PROP_LIBERTY_DISCO_SERVICE_URL = "org.netbeans.modules.identity.profile.api.configurator.libertydiscoserviceurl"; //NOI18N
082:
083: public static final String PROP_AS_ROOT = "org.netbeans.modules.identity.profile.api.configurator.asroot"; //NOI18N
084:
085: private static final String NAMING_SERVICE = "/namingservice"; //NOI18N
086:
087: private static final String IS_ALIVE_JSP = "/isAlive.jsp"; //NOI18N
088:
089: private static final String LIBERTY_DISCO_SERVICE = "/Liberty/disco"; //NOI18N
090:
091: private static final String AM_CONFIG_FILE = "/domains/domain1/config/AMConfig.properties"; //NOI18N
092:
093: private static HashMap<String, ServerProperties> instanceMap = new HashMap<String, ServerProperties>();
094:
095: public synchronized static ServerProperties getInstance(String id) {
096: ServerProperties instance = instanceMap.get(id);
097:
098: if (instance == null) {
099: instance = new ServerProperties(id);
100: instanceMap.put(id, instance);
101: }
102:
103: return instance;
104: }
105:
106: /** Creates a new instance of ServerProperties */
107: private ServerProperties(String id) throws ConfiguratorException {
108: super ();
109:
110: init(id);
111: }
112:
113: private void init(String id) {
114: String asRoot = id.substring(1, id.indexOf(']'));
115: String amConfigFile = asRoot + AM_CONFIG_FILE;
116: String[] segments = id.substring(id.indexOf(']') + 1)
117: .split(":"); //NOI18N
118: String host = segments[4];
119:
120: // RESOLVE:
121: // Need to figure a way to report errors.
122: // close the input stream?
123: // should cache
124: try {
125: load(new FileInputStream(amConfigFile.trim()));
126: setProperty(PROP_AM_CONFIG_FILE, amConfigFile);
127: setProperty(PROP_AS_ROOT, asRoot);
128: setProperty(PROP_ID, id);
129: setProperty(PROP_CONTEXT_ROOT, getContextRoot());
130: setProperty(PROP_HOST, host);
131: updateURLs();
132: } catch (Exception ex) {
133: throw new ConfiguratorException(NbBundle.getMessage(
134: ServerProperties.class, "TXT_InvalidAMConfigFile"));
135: }
136:
137: }
138:
139: public Object setProperty(String key, String value) {
140: Object prevValue = super .setProperty(key, value);
141:
142: updateInternalProperties(key);
143:
144: return prevValue;
145: }
146:
147: private void updateInternalProperties(String key) {
148: if (key.equals(PROP_PORT) || key.equals(PROP_HOST)
149: || key.equals(PROP_PROTOCOL)
150: || key.equals(PROP_CONTEXT_ROOT)) {
151: updateURLs();
152: } else if (key.equals(PROP_USERNAME)
153: || key.equals(PROP_PASSWORD)) {
154: }
155: }
156:
157: private void updateURLs() {
158: String port = getProperty(PROP_PORT);
159: String host = getProperty(PROP_HOST);
160: String protocol = getProperty(PROP_PROTOCOL);
161: String contextRoot = getProperty(PROP_CONTEXT_ROOT);
162:
163: String amConsoleURL = protocol + "://" + host + ":" + port
164: + "/" + contextRoot; //NOI18N
165: String namingServiceURL = amConsoleURL + NAMING_SERVICE;
166: String isAliveURL = amConsoleURL + IS_ALIVE_JSP;
167: String discoURL = amConsoleURL + LIBERTY_DISCO_SERVICE;
168:
169: setProperty(PROP_AM_CONSOLE_URL, amConsoleURL);
170: setProperty(PROP_NAMING_SERVICE_URL, namingServiceURL);
171: setProperty(PROP_IS_ALIVE_URL, isAliveURL);
172: setProperty(PROP_LIBERTY_DISCO_SERVICE_URL, discoURL);
173: }
174:
175: private String getContextRoot() {
176: //
177: // We get the context root by parsing the naming url.
178: // Is there a better way to do this?
179: //
180: String namingURL = getProperty(PROP_NAMING_SERVICE_URL);
181: String adminURL = namingURL.substring(0, namingURL.length()
182: - NAMING_SERVICE.length());
183: String contextRoot = adminURL.substring(adminURL
184: .lastIndexOf('/') + 1);
185:
186: return contextRoot;
187: }
188:
189: public boolean equals(Object obj) {
190: if (obj instanceof ServerProperties) {
191: ServerProperties properties = (ServerProperties) obj;
192:
193: if (!getProperty(PROP_HOST).equals(
194: properties.getProperty(PROP_HOST)))
195: return false;
196:
197: if (!getProperty(PROP_PORT).equals(
198: properties.getProperty(PROP_PORT)))
199: return false;
200:
201: if (!getProperty(PROP_USERNAME).equals(
202: properties.getProperty(PROP_USERNAME)))
203: return false;
204:
205: if (!getProperty(PROP_PASSWORD).equals(
206: properties.getProperty(PROP_PASSWORD)))
207: return false;
208:
209: if (!getProperty(PROP_CONTEXT_ROOT).equals(
210: properties.getProperty(PROP_CONTEXT_ROOT)))
211: return false;
212:
213: return true;
214:
215: /* RESOLVE: Should we check all the properties
216: if (properties.size() != this.size())
217: return false;
218:
219: Enumeration propNames = this.propertyNames();
220:
221: while (propNames.hasMoreElements()) {
222: String propName = (String) propNames.nextElement();
223: String value = properties.getProperty(propName);
224:
225: if (value == null) return false;
226:
227: if (!value.equals(this.getProperty(propName)))
228: return false;
229: }
230:
231: return true;
232: */
233: }
234:
235: return false;
236: }
237:
238: public int hashCode() {
239: int hashCode = 0;
240: Object value = null;
241:
242: if ((value = getProperty(PROP_HOST)) != null) {
243: hashCode += value.hashCode();
244: }
245:
246: if ((value = getProperty(PROP_PORT)) != null) {
247: hashCode += value.hashCode();
248: }
249:
250: if ((value = getProperty(PROP_USERNAME)) != null) {
251: hashCode += value.hashCode();
252: }
253:
254: if ((value = getProperty(PROP_PASSWORD)) != null) {
255: hashCode += value.hashCode();
256: }
257:
258: if ((value = getProperty(PROP_CONTEXT_ROOT)) != null) {
259: hashCode += value.hashCode();
260: }
261:
262: return hashCode;
263: }
264:
265: public Object clone() {
266: Object clone = super .clone();
267:
268: Enumeration propNames = this .propertyNames();
269: while (propNames.hasMoreElements()) {
270: String name = (String) propNames.nextElement();
271:
272: ((ServerProperties) clone).setProperty(name, this
273: .getProperty(name));
274: }
275:
276: return clone;
277: }
278:
279: public String toString() {
280: return getProperty(PROP_ID) + " (" + getProperty(PROP_HOST) + //NOI18N
281: ":" + getProperty(PROP_PORT) + ")"; //NOI18N
282: };
283:
284: }
|