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:
042: package org.netbeans.modules.identity.server.manager.api;
043:
044: import java.beans.*;
045: import java.io.IOException;
046: import java.io.Serializable;
047: import java.net.HttpURLConnection;
048: import java.net.MalformedURLException;
049: import java.net.URL;
050: import org.netbeans.modules.identity.profile.api.configurator.ServerProperties;
051:
052: /**
053: * This class represents an instance of the AM server.
054: *
055: * Created on June 14, 2006, 11:10 AM
056: *
057: * @author ptliu
058: */
059: public class ServerInstance extends Object implements Serializable {
060:
061: public static final String PROP_ID = "id"; //NOI18N
062:
063: public static final String PROP_HOST = "host"; //NOI18N
064:
065: public static final String PROP_PORT = "port"; //NOI18N
066:
067: public static final String PROP_CONTEXT_ROOT = "contextRoot"; //NOI18N
068:
069: public static final String PROP_USERNAME = "userName"; //NOI18N
070:
071: public static final String PROP_PASSWORD = "password"; //NOI18N
072:
073: public static final String LOCAL_HOST = "localhost"; //NOI18N
074:
075: private String id;
076: private String host;
077: private String port;
078: private String contextRoot;
079: private String userName;
080: private String password;
081: private ServerProperties properties;
082: private PropertyChangeSupport propertySupport;
083:
084: public ServerInstance() {
085: propertySupport = new PropertyChangeSupport(this );
086: }
087:
088: public String getID() {
089: return id;
090: }
091:
092: public String getHost() {
093: return host;
094: }
095:
096: public String getPort() {
097: return port;
098: }
099:
100: public String getContextRoot() {
101: return contextRoot;
102: }
103:
104: public String getUserName() {
105: return userName;
106: }
107:
108: public String getPassword() {
109: return password;
110: }
111:
112: void setID(String id) {
113: this .id = id;
114: }
115:
116: public void setHost(String value) {
117: String oldValue = host;
118: host = value;
119: propertySupport.firePropertyChange(PROP_HOST, oldValue, host);
120: }
121:
122: public void setPort(String value) {
123: String oldValue = port;
124: port = value;
125: propertySupport.firePropertyChange(PROP_PORT, oldValue, host);
126: }
127:
128: public void setContextRoot(String value) {
129: String oldValue = contextRoot;
130: contextRoot = value;
131: propertySupport.firePropertyChange(PROP_CONTEXT_ROOT, oldValue,
132: contextRoot);
133: }
134:
135: public void setUserName(String value) {
136: String oldValue = userName;
137: userName = value;
138: propertySupport.firePropertyChange(PROP_USERNAME, oldValue,
139: userName);
140: }
141:
142: public void setPassword(String value) {
143: String oldValue = password;
144: password = value;
145: propertySupport.firePropertyChange(PROP_PASSWORD, oldValue,
146: password);
147: }
148:
149: public boolean isLocal() {
150: return (getHost().equals(LOCAL_HOST));
151: }
152:
153: public void addPropertyChangeListener(
154: PropertyChangeListener listener) {
155: propertySupport.addPropertyChangeListener(listener);
156: }
157:
158: public void removePropertyChangeListener(
159: PropertyChangeListener listener) {
160: propertySupport.removePropertyChangeListener(listener);
161: }
162:
163: public ServerProperties getServerProperties() {
164: if (properties == null) {
165: properties = ServerProperties.getInstance(getID());
166: }
167:
168: //if (!isLocal()) {
169: properties.setProperty(ServerProperties.PROP_HOST, host);
170: properties.setProperty(ServerProperties.PROP_PORT, port);
171: properties.setProperty(ServerProperties.PROP_CONTEXT_ROOT,
172: contextRoot);
173: properties
174: .setProperty(ServerProperties.PROP_USERNAME, userName);
175: properties
176: .setProperty(ServerProperties.PROP_PASSWORD, password);
177: //}
178:
179: return properties;
180: }
181:
182: public String getAdminURL() {
183: return getServerProperties().getProperty(
184: ServerProperties.PROP_AM_CONSOLE_URL);
185: }
186:
187: public void remove() {
188: ServerManager.getDefault().removeServerInstance(this );
189: }
190:
191: public boolean isRunning() {
192: HttpURLConnection conn = null;
193:
194: try {
195: URL isAliveURL = new URL(getServerProperties().getProperty(
196: ServerProperties.PROP_IS_ALIVE_URL));
197: conn = (HttpURLConnection) isAliveURL.openConnection();
198:
199: if (conn.getResponseCode() == 200) {
200: return true;
201: }
202: } catch (MalformedURLException ex) {
203: //ex.printStackTrace();
204: } catch (IOException ex) {
205: //ex.printStackTrace();
206: } finally {
207: if (conn != null) {
208: conn.disconnect();
209: }
210: }
211:
212: return false;
213: }
214:
215: public String toString() {
216: return "ServerInstance: id = " + id + " host = "
217: + host
218: + " port = " //NOI18N
219: + port + " contextRoot = " + contextRoot
220: + " userName = " //NOI18N
221: + userName + " password = " + password; //NOI18N
222: }
223:
224: }
|