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: * ServerManager.java
043: *
044: * Created on June 14, 2006, 1:05 AM
045: *
046: * To change this template, choose Tools | Template Manager
047: * and open the template in the editor.
048: */
049:
050: package org.netbeans.modules.identity.server.manager.api;
051:
052: import java.io.IOException;
053: import java.util.Collection;
054: import java.util.Collections;
055: import java.util.HashMap;
056: import java.util.HashSet;
057: import java.util.Map;
058: import java.util.Set;
059: import org.netbeans.modules.identity.profile.api.configurator.ServerProperties;
060: import org.netbeans.modules.identity.server.manager.spi.ServerInstanceListener;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.FileUtil;
063: import org.openide.filesystems.Repository;
064: import org.openide.util.Lookup;
065:
066: /**
067: * This class manages multiples instances of ServerInstance.
068: *
069: * Created on June 14, 2006, 1:05 AM
070: *
071: * @author ptliu
072: */
073: public class ServerManager {
074: private static String DIR_CONFIGURED_SERVERS = "/Identity/ConfiguredServers"; //NOI18N
075:
076: private static ServerManager instance;
077:
078: private Map<String, ServerInstance> instancesMap;
079:
080: private Set<ServerInstanceListener> listeners;
081:
082: /** Creates a new instance of ServerManager */
083: private ServerManager() {
084: init();
085: }
086:
087: public static ServerManager getDefault() {
088: if (instance == null) {
089: instance = new ServerManager();
090: }
091:
092: return instance;
093: }
094:
095: public void init() {
096: listeners = new HashSet<ServerInstanceListener>();
097: instancesMap = new HashMap<String, ServerInstance>();
098:
099: FileObject dir = getConfiguredServersDirectory();
100: //dir.addFileChangeListener(new InstanceInstallListener());
101: FileObject[] ch = dir.getChildren();
102:
103: for (int i = 0; i < ch.length; i++) {
104: //System.out.println("addInstance() ch = " + ch[i]);
105: ServerInstance instance = convertToServerInstance(ch[i]);
106: addServerInstanceInternal(instance);
107: }
108: }
109:
110: public Collection<ServerInstance> getServerInstances() {
111: return Collections
112: .unmodifiableCollection(instancesMap.values());
113: }
114:
115: public ServerInstance getServerInstance(ServerProperties properties) {
116: String id = properties.getProperty(ServerProperties.PROP_ID);
117:
118: ServerInstance instance = instancesMap.get(id);
119:
120: if (instance == null) {
121: instance = createServerInstance(properties);
122: addServerInstance(instance);
123: }
124:
125: return instance;
126: }
127:
128: public ServerInstance getServerInstance(String id) {
129: ServerProperties properties = ServerProperties.getInstance(id);
130:
131: return getServerInstance(properties);
132:
133: }
134:
135: private ServerInstance createServerInstance(
136: ServerProperties properties) {
137: ServerInstance instance = new ServerInstance();
138:
139: instance
140: .setID(properties.getProperty(ServerProperties.PROP_ID));
141: instance.setHost(properties
142: .getProperty(ServerProperties.PROP_HOST));
143: instance.setPort(properties
144: .getProperty(ServerProperties.PROP_PORT));
145: instance.setContextRoot(properties
146: .getProperty(ServerProperties.PROP_CONTEXT_ROOT));
147: instance.setUserName(properties
148: .getProperty(ServerProperties.PROP_USERNAME));
149: instance.setPassword(properties
150: .getProperty(ServerProperties.PROP_PASSWORD));
151:
152: return instance;
153: }
154:
155: public boolean addServerInstance(ServerInstance instance) {
156: //writeInstanceToFile(instance);
157: addServerInstanceInternal(instance);
158: fireServerInstanceAdded(instance);
159:
160: return true;
161: }
162:
163: public void removeServerInstance(ServerInstance instance) {
164: try {
165: removeInstanceFromFile(instance);
166: removeServerInstanceInternal(instance);
167: fireServerInstanceRemoved(instance);
168: } catch (IOException ex) {
169: ex.printStackTrace();
170: }
171: }
172:
173: public void addServerInstanceListener(
174: ServerInstanceListener listener) {
175: listeners.add(listener);
176: }
177:
178: public void removeServerInstanceListener(
179: ServerInstanceListener listener) {
180: listeners.remove(listener);
181: }
182:
183: public void fireServerInstanceAdded(ServerInstance instance) {
184: String id = instance.getID();
185:
186: for (ServerInstanceListener l : listeners) {
187: l.instanceAdded(id);
188: }
189: }
190:
191: public void fireServerInstanceRemoved(ServerInstance instance) {
192: String id = instance.getID();
193:
194: for (ServerInstanceListener l : listeners) {
195: l.instanceRemoved(id);
196: }
197: }
198:
199: private void addServerInstanceInternal(ServerInstance instance) {
200: instancesMap.put(instance.getID(), instance);
201:
202: //TODO: Need to add change listeners
203: }
204:
205: private void removeServerInstanceInternal(ServerInstance instance) {
206: instancesMap.remove(instance.getID());
207: }
208:
209: private boolean serverInstanceExists(ServerInstance instance) {
210: if (instancesMap.get(instance.getID()) == null) {
211: return false;
212: }
213:
214: return true;
215: }
216:
217: private ServerInstance convertToServerInstance(FileObject fObj) {
218: ServerInstance instance = new ServerInstance();
219:
220: instance.setID((String) fObj
221: .getAttribute(ServerInstance.PROP_ID));
222: instance.setHost((String) fObj
223: .getAttribute(ServerInstance.PROP_HOST));
224: instance.setPort((String) fObj
225: .getAttribute(ServerInstance.PROP_PORT));
226: instance.setContextRoot((String) fObj
227: .getAttribute(ServerInstance.PROP_CONTEXT_ROOT));
228: instance.setUserName((String) fObj
229: .getAttribute(ServerInstance.PROP_USERNAME));
230: instance.setPassword((String) fObj
231: .getAttribute(ServerInstance.PROP_PASSWORD));
232:
233: return instance;
234: }
235:
236: public synchronized void writeInstanceToFile(ServerInstance instance)
237: throws IOException {
238: FileObject dir = getConfiguredServersDirectory();
239: FileObject instanceFOs[] = dir.getChildren();
240: FileObject instanceFO = null;
241: String id = instance.getID();
242:
243: if (id != null) {
244: for (int i = 0; i < instanceFOs.length; i++) {
245: if (id.equals(instanceFOs[i]
246: .getAttribute(ServerInstance.PROP_ID)))
247: instanceFO = instanceFOs[i];
248: }
249: }
250:
251: if (instanceFO == null) {
252: String fileName = FileUtil.findFreeFileName(dir,
253: "instance", null); //NOI18N
254: instanceFO = dir.createData(fileName);
255: }
256:
257: instanceFO.setAttribute(ServerInstance.PROP_ID, instance
258: .getID());
259: instanceFO.setAttribute(ServerInstance.PROP_HOST, instance
260: .getHost());
261: instanceFO.setAttribute(ServerInstance.PROP_PORT, instance
262: .getPort());
263: instanceFO.setAttribute(ServerInstance.PROP_CONTEXT_ROOT,
264: instance.getContextRoot());
265: instanceFO.setAttribute(ServerInstance.PROP_USERNAME, instance
266: .getUserName());
267: instanceFO.setAttribute(ServerInstance.PROP_PASSWORD, instance
268: .getPassword());
269: }
270:
271: private void removeInstanceFromFile(ServerInstance instance)
272: throws IOException {
273: FileObject dir = getConfiguredServersDirectory();
274: FileObject instanceFOs[] = dir.getChildren();
275: FileObject instanceFO = null;
276: String id = instance.getID();
277:
278: if (id != null) {
279: for (int i = 0; i < instanceFOs.length; i++) {
280: if (id.equals(instanceFOs[i]
281: .getAttribute(ServerInstance.PROP_ID)))
282: instanceFO = instanceFOs[i];
283: }
284: }
285:
286: if (instanceFO != null)
287: instanceFO.delete();
288: }
289:
290: public static FileObject getInstanceFileObject(String id) {
291: FileObject dir = getConfiguredServersDirectory();
292: FileObject[] servers = dir.getChildren();
293:
294: for (int i = 0; i < servers.length; i++) {
295: String val = (String) servers[i]
296: .getAttribute(ServerInstance.PROP_ID);
297: if (val != null && val.equals(id))
298: return servers[i];
299: }
300: return null;
301: }
302:
303: private static FileObject getConfiguredServersDirectory() {
304: Repository rep = (Repository) Lookup.getDefault().lookup(
305: Repository.class);
306: return rep.getDefaultFileSystem().findResource(
307: DIR_CONFIGURED_SERVERS);
308: }
309: }
|