Source Code Cross Referenced for ServerRegistry.java in  » 6.0-JDK-Core » j2eeserver » org » netbeans » modules » j2ee » deployment » impl » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » j2eeserver » org.netbeans.modules.j2ee.deployment.impl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.j2ee.deployment.impl;
043
044        import java.util.logging.Logger;
045        import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
046        import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
047        import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceCreationException;
048        import javax.enterprise.deploy.spi.DeploymentManager;
049        import org.openide.filesystems.*;
050        import org.openide.*;
051        import org.openide.util.Lookup;
052        import org.openide.util.NbBundle;
053
054        import java.util.*;
055        import java.io.*;
056        import java.util.ArrayList;
057        import java.util.logging.Level;
058        import org.netbeans.modules.j2ee.deployment.devmodules.spi.InstanceListener;
059        import org.netbeans.modules.j2ee.deployment.profiler.spi.Profiler;
060        import org.openide.modules.InstalledFileLocator;
061
062        //import java.util.logging.*;
063
064        public final class ServerRegistry implements  java.io.Serializable {
065
066            private static final Logger LOGGER = Logger
067                    .getLogger(ServerRegistry.class.getName());
068
069            public static final String DIR_INSTALLED_SERVERS = "/J2EE/InstalledServers"; //NOI18N
070            public static final String DIR_JSR88_PLUGINS = "/J2EE/DeploymentPlugins"; //NOI18N
071            public static final String URL_ATTR = InstanceProperties.URL_ATTR;
072            public static final String USERNAME_ATTR = InstanceProperties.USERNAME_ATTR;
073            public static final String PASSWORD_ATTR = InstanceProperties.PASSWORD_ATTR;
074            public static final String FILE_DEFAULT_INSTANCE = "DefaultInstance.settings"; //NOI18N
075            public static final String J2EE_DEFAULT_SERVER = "j2ee.defaultServer"; //NOI18N
076            public static final String TARGETNAME_ATTR = "targetName"; //NOI18N
077            public static final String SERVER_NAME = "serverName"; //NOI18N
078            private static ServerRegistry instance = null;
079
080            public synchronized static ServerRegistry getInstance() {
081                if (instance == null)
082                    instance = new ServerRegistry();
083                return instance;
084
085                //PENDING need to get this from lookup
086                //    return (ServerRegistry) Lookup.getDefault().lookup(ServerRegistry.class);
087            }
088
089            /** Utility method that returns true if the ServerRegistry was initialized
090             * during the current IDE session and false otherwise.
091             */
092            public synchronized static boolean wasInitialized() {
093                return instance != null && instance.servers != null
094                        && instance.instances != null;
095            }
096
097            private transient Map servers = null;
098            private transient Map instances = null;
099            private transient Collection pluginListeners = new HashSet();
100            private transient Collection instanceListeners = new ArrayList();
101            private transient InstanceListener[] instanceListenersArray;
102
103            // This is the serializable portion of ServerRegistry
104            private ServerString defaultInstance;
105
106            private ServerRegistry() {
107                super ();
108            }
109
110            private synchronized void init() {
111                LOGGER.log(Level.FINE, "Entering registry initialization"); // NOI18N
112
113                if (servers != null && instances != null) {
114                    return;
115                }
116
117                servers = new HashMap();
118                instances = new HashMap();
119
120                Repository rep = (Repository) Lookup.getDefault().lookup(
121                        Repository.class);
122                FileObject dir = rep.getDefaultFileSystem().findResource(
123                        DIR_JSR88_PLUGINS);
124                if (dir != null) {
125                    LOGGER.log(Level.FINE, "Loading server plugins"); // NOI18N
126                    dir.addFileChangeListener(new PluginInstallListener());
127                    FileObject[] ch = dir.getChildren();
128                    for (int i = 0; i < ch.length; i++) {
129                        addPlugin(ch[i]);
130                    }
131                    LOGGER.log(Level.FINE, "Loading server instances"); // NOI18N
132                    dir = rep.getDefaultFileSystem().findResource(
133                            DIR_INSTALLED_SERVERS);
134                    dir.addFileChangeListener(new InstanceInstallListener());
135                    ch = dir.getChildren();
136                    for (int i = 0; i < ch.length; i++) {
137                        addInstance(ch[i]);
138                    }
139                } else {
140                    LOGGER
141                            .log(Level.WARNING,
142                                    "No DIR_JSR88_PLUGINS folder found, no server plugins will be availabe"); // NOI18N
143                }
144            }
145
146            private Map serversMap() {
147                init();
148                return servers;
149            }
150
151            private synchronized Map instancesMap() {
152                init();
153                return instances;
154            }
155
156            private void addPlugin(FileObject fo) {
157                String name = ""; //NOI18N
158                try {
159                    if (fo.isFolder()) {
160                        name = fo.getName();
161                        Server server = null;
162                        synchronized (this ) {
163                            if (serversMap().containsKey(name)) {
164                                return;
165                            }
166                            server = new Server(fo);
167                            serversMap().put(name, server);
168                        }
169                        if (server != null) {
170                            firePluginListeners(server, true);
171                            fetchInstances(server);
172                        }
173                    }
174                } catch (Exception e) {
175                    LOGGER.log(Level.WARNING, "Plugin " + name
176                            + " installation failed"); //NOI18N
177                    LOGGER.log(Level.INFO, null, e);
178                }
179            }
180
181            private void fetchInstances(Server server) {
182                Repository rep = (Repository) Lookup.getDefault().lookup(
183                        Repository.class);
184                FileObject dir = rep.getDefaultFileSystem().findResource(
185                        DIR_INSTALLED_SERVERS);
186                FileObject[] ch = dir.getChildren();
187                for (int i = 0; i < ch.length; i++) {
188                    String url = (String) ch[i].getAttribute(URL_ATTR);
189                    if (url != null && server.handlesUri(url)) {
190                        addInstance(ch[i]);
191                    }
192                }
193            }
194
195            private void removePlugin(FileObject fo) {
196                Server server = null;
197                synchronized (this ) {
198                    String name = fo.getName();
199
200                    server = (Server) serversMap().get(name);
201                    if (server != null) {
202                        // remove all registered server instances of the given server type
203                        ServerInstance[] instances = getServerInstances();
204                        for (int i = 0; i < instances.length; i++) {
205                            ServerInstance si = instances[i];
206                            if (server.equals(si.getServer())) {
207                                removeServerInstance(si.getUrl());
208                            }
209                        }
210                    }
211                    serversMap().remove(name);
212                }
213                if (server != null) {
214                    firePluginListeners(server, false);
215                }
216            }
217
218            class PluginInstallListener extends LayerListener {
219                public void fileFolderCreated(FileEvent fe) {
220                    super .fileFolderCreated(fe);
221                    addPlugin(fe.getFile());
222                }
223
224                public void fileDeleted(FileEvent fe) {
225                    super .fileDeleted(fe);
226                    removePlugin(fe.getFile());
227                }
228            }
229
230            class InstanceInstallListener extends LayerListener {
231                public void fileDataCreated(FileEvent fe) {
232                    super .fileDataCreated(fe);
233                    addInstance(fe.getFile());
234                }
235                // PENDING should support removing of instances?
236            }
237
238            class LayerListener implements  FileChangeListener {
239
240                public void fileAttributeChanged(FileAttributeEvent fae) {
241                }
242
243                public void fileChanged(FileEvent fe) {
244                }
245
246                public void fileFolderCreated(FileEvent fe) {
247                }
248
249                public void fileRenamed(FileRenameEvent fe) {
250                }
251
252                public void fileDataCreated(FileEvent fe) {
253                }
254
255                public void fileDeleted(FileEvent fe) {
256                }
257
258            }
259
260            public Collection getServers() {
261                return serversMap().values();
262            }
263
264            public synchronized Collection getInstances() {
265                return new ArrayList(instancesMap().values());
266            }
267
268            public synchronized String[] getInstanceURLs() {
269                return (String[]) instancesMap().keySet().toArray(
270                        new String[instancesMap().size()]);
271            }
272
273            public void checkInstanceAlreadyExists(String url)
274                    throws InstanceCreationException {
275                if (getServerInstance(url) != null) {
276                    String msg = NbBundle.getMessage(ServerRegistry.class,
277                            "MSG_InstanceAlreadyExists", url);
278                    throw new InstanceCreationException(msg);
279                }
280            }
281
282            public void checkInstanceExists(String url) {
283                if (getServerInstance(url) == null) {
284                    String msg = NbBundle.getMessage(ServerRegistry.class,
285                            "MSG_InstanceNotExists", url);
286                    throw new IllegalArgumentException(msg);
287                }
288            }
289
290            public Server getServer(String name) {
291                return (Server) serversMap().get(name);
292            }
293
294            public void addPluginListener(PluginListener pl) {
295                pluginListeners.add(pl);
296            }
297
298            public synchronized ServerInstance getServerInstance(String url) {
299                return (ServerInstance) instancesMap().get(url);
300            }
301
302            public void removeServerInstance(String url) {
303                if (url == null)
304                    return;
305
306                // Make sure defaultInstance cache is reset
307                ServerString def = getDefaultInstance();
308                if (def != null && url.equals(def.getUrl())) {
309                    defaultInstance = null;
310                }
311
312                ServerInstance instance = null;
313                synchronized (this ) {
314                    instance = (ServerInstance) instancesMap().remove(url);
315                }
316                if (instance != null) {
317                    fireInstanceListeners(url, false);
318                    removeInstanceFromFile(url);
319                }
320                ServerString newinst = getDefaultInstance(false);
321                fireDefaultInstance(def != null ? def.getUrl() : null,
322                        newinst != null ? newinst.getUrl() : null);
323            }
324
325            public synchronized ServerInstance[] getServerInstances() {
326                ServerInstance[] ret = new ServerInstance[instancesMap().size()];
327                instancesMap().values().toArray(ret);
328                return ret;
329            }
330
331            public static FileObject getInstanceFileObject(String url) {
332                Repository rep = (Repository) Lookup.getDefault().lookup(
333                        Repository.class);
334                FileObject[] installedServers = rep.getDefaultFileSystem()
335                        .findResource(DIR_INSTALLED_SERVERS).getChildren();
336                for (int i = 0; i < installedServers.length; i++) {
337                    String val = (String) installedServers[i]
338                            .getAttribute(URL_ATTR);
339                    if (val != null && val.equals(url))
340                        return installedServers[i];
341                }
342                return null;
343            }
344
345            /**
346             * Add a new server instance in the server registry.
347             *
348             * @param  url URL to access deployment manager.
349             * @param  username username used by the deployment manager.
350             * @param  password password used by the deployment manager.
351             * @param  displayName display name wich represents server instance in IDE.
352             * @param initialProperties any other properties to set during the instance creation.
353             *             If the map contains any of InstanceProperties.URL_ATTR,
354             *             InstanceProperties.USERNAME_ATTR, InstanceProperties.PASSWORD_ATTR
355             *             or InstanceProperties.DISPLAY_NAME_ATTR they will be ignored
356             *             - the explicit parameter values are always used.
357             *             <code>null</code> is accepted.
358             *
359             * @throws InstanceCreationException when instance with same url is already
360             *         registered.
361             */
362            public void addInstance(String url, String username,
363                    String password, String displayName, boolean withoutUI,
364                    Map<String, String> initialproperties)
365                    throws InstanceCreationException {
366                // should never have empty url; UI should have prevented this
367                if (url == null || url.equals("")) { //NOI18N
368                    LOGGER.log(Level.INFO, NbBundle.getMessage(
369                            ServerRegistry.class, "MSG_EmptyUrl"));
370                    return;
371                }
372
373                checkInstanceAlreadyExists(url);
374                if (!addInstanceImpl(url, username, password, displayName,
375                        withoutUI, initialproperties)) {
376                    throw new InstanceCreationException(NbBundle.getMessage(
377                            ServerRegistry.class, "MSG_FailedToCreateInstance",
378                            displayName));
379                }
380            }
381
382            private synchronized void writeInstanceToFile(String url,
383                    String username, String password) throws IOException {
384                if (url == null) {
385                    Logger.getLogger("global").log(
386                            Level.SEVERE,
387                            NbBundle.getMessage(ServerRegistry.class,
388                                    "MSG_NullUrl"));
389                    return;
390                }
391                Repository rep = (Repository) Lookup.getDefault().lookup(
392                        Repository.class);
393                FileObject dir = rep.getDefaultFileSystem().findResource(
394                        DIR_INSTALLED_SERVERS);
395                FileObject instanceFOs[] = dir.getChildren();
396                FileObject instanceFO = null;
397                for (int i = 0; i < instanceFOs.length; i++) {
398                    if (url.equals(instanceFOs[i].getAttribute(URL_ATTR)))
399                        instanceFO = instanceFOs[i];
400                }
401                String name = FileUtil.findFreeFileName(dir, "instance", null);
402                if (instanceFO == null)
403                    instanceFO = dir.createData(name);
404                instanceFO.setAttribute(URL_ATTR, url);
405                instanceFO.setAttribute(USERNAME_ATTR, username);
406                instanceFO.setAttribute(PASSWORD_ATTR, password);
407            }
408
409            private synchronized void removeInstanceFromFile(String url) {
410                FileObject instanceFO = getInstanceFileObject(url);
411                if (instanceFO == null)
412                    return;
413                try {
414                    instanceFO.delete();
415                } catch (IOException ioe) {
416                    Logger.getLogger("global").log(Level.INFO, null, ioe);
417                }
418            }
419
420            /**
421             * Add a new server instance in the server registry.
422             *
423             * @param url URL to access deployment manager.
424             * @param username username used by the deployment manager.
425             * @param password password used by the deployment manager.
426             * @param displayName display name wich represents server instance in IDE.
427             * @param initialProperties any other properties to set during the instance creation.
428             *             If the map contains any of InstanceProperties.URL_ATTR,
429             *             InstanceProperties.USERNAME_ATTR, InstanceProperties.PASSWORD_ATTR
430             *             or InstanceProperties.DISPLAY_NAME_ATTR they will be ignored
431             *             - the explicit parameter values are always used.
432             *             <code>null</code> is accepted.
433             *
434             * @return <code>true</code> if the server instance was created successfully,
435             *             <code>false</code> otherwise.
436             */
437            private synchronized boolean addInstanceImpl(String url,
438                    String username, String password, String displayName,
439                    boolean withoutUI, Map<String, String> initialProperties) {
440                if (instancesMap().containsKey(url)) {
441                    return false;
442                }
443
444                Map<String, String> properties = cleanInitialProperties(initialProperties);
445
446                for (Iterator i = serversMap().values().iterator(); i.hasNext();) {
447                    Server server = (Server) i.next();
448                    try {
449                        if (server.handlesUri(url)) {
450                            ServerInstance instance = new ServerInstance(
451                                    server, url);
452                            // PENDING persist url/password in ServerString as well
453                            instancesMap().put(url, instance);
454                            // try to create a disconnected deployment manager to see
455                            // whether the instance is not corrupted - see #46929
456                            writeInstanceToFile(url, username, password);
457                            instance.getInstanceProperties().setProperty(
458                                    InstanceProperties.REGISTERED_WITHOUT_UI,
459                                    Boolean.toString(withoutUI));
460                            if (displayName != null) {
461                                instance.getInstanceProperties().setProperty(
462                                        InstanceProperties.DISPLAY_NAME_ATTR,
463                                        displayName);
464                            }
465
466                            for (Map.Entry<String, String> entry : properties
467                                    .entrySet()) {
468                                instance.getInstanceProperties().setProperty(
469                                        entry.getKey(), entry.getValue());
470                            }
471
472                            DeploymentManager manager = server
473                                    .getDisconnectedDeploymentManager(url);
474                            // FIXME this shouldn't be called in synchronized block
475                            if (manager != null) {
476                                fireInstanceListeners(url, true);
477                                return true;
478                            } else {
479                                removeInstanceFromFile(url);
480                                instancesMap().remove(url);
481                            }
482                        }
483                    } catch (Exception e) {
484                        if (instancesMap().containsKey(url)) {
485                            removeInstanceFromFile(url);
486                            instancesMap().remove(url);
487                        }
488                        LOGGER.log(Level.INFO, null, e);
489                    }
490                }
491                return false;
492            }
493
494            private Map<String, String> cleanInitialProperties(
495                    Map<String, String> initialProperties) {
496                if (initialProperties == null) {
497                    return Collections.<String, String> emptyMap();
498                }
499
500                Map<String, String> properties = new HashMap(initialProperties);
501                properties.remove(InstanceProperties.URL_ATTR);
502                properties.remove(InstanceProperties.USERNAME_ATTR);
503                properties.remove(InstanceProperties.PASSWORD_ATTR);
504                properties.remove(InstanceProperties.DISPLAY_NAME_ATTR);
505                properties.remove(InstanceProperties.REGISTERED_WITHOUT_UI);
506                return properties;
507            }
508
509            public void addInstance(FileObject fo) {
510                String url = (String) fo.getAttribute(URL_ATTR);
511                String username = (String) fo.getAttribute(USERNAME_ATTR);
512                String password = (String) fo.getAttribute(PASSWORD_ATTR);
513                String displayName = (String) fo
514                        .getAttribute(InstanceProperties.DISPLAY_NAME_ATTR);
515                String withoutUI = (String) fo
516                        .getAttribute(InstanceProperties.REGISTERED_WITHOUT_UI);
517                boolean withoutUIFlag = withoutUI == null ? false : Boolean
518                        .valueOf(withoutUI);
519                addInstanceImpl(url, username, password, displayName,
520                        withoutUIFlag, null);
521            }
522
523            public Collection getInstances(InstanceListener il) {
524                if (il != null) {
525                    synchronized (instanceListeners) {
526                        instanceListenersArray = null;
527                        instanceListeners.add(il);
528                    }
529                }
530                return getInstances();
531            }
532
533            public void addInstanceListener(InstanceListener il) {
534                synchronized (instanceListeners) {
535                    instanceListenersArray = null;
536                    instanceListeners.add(il);
537                }
538            }
539
540            public void removeInstanceListener(InstanceListener il) {
541                synchronized (instanceListeners) {
542                    instanceListenersArray = null;
543                    instanceListeners.remove(il);
544                }
545            }
546
547            public synchronized void removePluginListener(PluginListener pl) {
548                pluginListeners.remove(pl);
549            }
550
551            private void firePluginListeners(Server server, boolean add) {
552                LOGGER.log(Level.FINE, "Firing plugin listener"); // NOI18N
553                for (Iterator i = pluginListeners.iterator(); i.hasNext();) {
554                    PluginListener pl = (PluginListener) i.next();
555                    if (add)
556                        pl.serverAdded(server);
557                    else
558                        pl.serverRemoved(server);
559                }
560                configNamesByType = null;
561            }
562
563            private InstanceListener[] getInstanceListeners() {
564                InstanceListener[] retValue = null;
565                synchronized (instanceListeners) {
566                    retValue = instanceListenersArray;
567                    if (retValue == null) {
568                        retValue = (InstanceListener[]) instanceListeners
569                                .toArray(new InstanceListener[instanceListeners
570                                        .size()]);
571                        instanceListenersArray = retValue;
572                    }
573                }
574                return retValue;
575            }
576
577            private void fireInstanceListeners(String instance, boolean add) {
578                InstanceListener[] instListeners = getInstanceListeners();
579                for (int i = 0; i < instListeners.length; i++) {
580                    if (add) {
581                        instListeners[i].instanceAdded(instance);
582                    } else {
583                        instListeners[i].instanceRemoved(instance);
584                    }
585                }
586            }
587
588            private void fireDefaultInstance(String oldInstance,
589                    String newInstance) {
590                InstanceListener[] instListeners = getInstanceListeners();
591                for (int i = 0; i < instListeners.length; i++) {
592                    instListeners[i].changeDefaultInstance(oldInstance,
593                            newInstance);
594                }
595            }
596
597            public void setDefaultInstance(ServerString instance) {
598                if (instance != null && instance.equals(defaultInstance)) {
599                    return;
600                }
601
602                if (instance == null) {
603                    removeDefaultInstanceFile();
604                    ServerString oldValue = defaultInstance;
605                    defaultInstance = null;
606                    fireDefaultInstance(oldValue != null ? oldValue.getUrl()
607                            : null, null);
608                } else {
609                    if (ServerStringConverter.writeServerInstance(instance,
610                            DIR_INSTALLED_SERVERS, FILE_DEFAULT_INSTANCE)) {
611                        ServerString oldValue = defaultInstance;
612                        defaultInstance = instance;
613                        fireDefaultInstance(oldValue != null ? oldValue
614                                .getUrl() : null, instance.getUrl());
615                    }
616                }
617            }
618
619            static private void removeDefaultInstanceFile() {
620                FileLock lock = null;
621                Writer writer = null;
622                try {
623                    String pathName = DIR_INSTALLED_SERVERS + "/"
624                            + FILE_DEFAULT_INSTANCE; // NOI18N
625                    FileObject fo = Repository.getDefault()
626                            .getDefaultFileSystem().findResource(pathName);
627                    if (fo != null)
628                        fo.delete();
629                } catch (Exception ioe) {
630                    Logger.getLogger("global").log(Level.WARNING, null, ioe);
631                }
632            }
633
634            private ServerString getInstallerDefaultPlugin() {
635                File propFile = InstalledFileLocator.getDefault().locate(
636                        "config/install.properties", null, false); // NOI18N
637                Properties installProp = readProperties(propFile); //NOI18N
638
639                String j2eeDefaultServerFileName = installProp
640                        .getProperty(J2EE_DEFAULT_SERVER);
641                if (j2eeDefaultServerFileName == null)
642                    return null;
643
644                File serverFile = InstalledFileLocator.getDefault().locate(
645                        j2eeDefaultServerFileName, null, false);
646                Properties defaultServerProp = readProperties(serverFile);
647                String serverName = defaultServerProp.getProperty(SERVER_NAME);
648                String url = defaultServerProp.getProperty(URL_ATTR);
649                String user = defaultServerProp.getProperty(USERNAME_ATTR);
650                String password = defaultServerProp.getProperty(PASSWORD_ATTR);
651                String targetName = defaultServerProp
652                        .getProperty(TARGETNAME_ATTR);
653
654                Map<String, String> defaults = new HashMap<String, String>();
655                for (Enumeration e = defaultServerProp.propertyNames(); e
656                        .hasMoreElements();) {
657                    String name = (String) e.nextElement();
658                    String value = defaultServerProp.getProperty(name);
659                    if (value != null) {
660                        defaults.put(name, value);
661                    }
662                }
663
664                try {
665                    if (url != null) {
666                        InstanceProperties instProp = InstanceProperties
667                                .getInstanceProperties(url);
668                        if (instProp == null) {
669                            instProp = InstanceProperties
670                                    .createInstanceProperties(url, user,
671                                            password, null, defaults);
672                        }
673                        //instProp.setProperties(defaultServerProp);
674
675                        ServerInstance inst = getServerInstance(url);
676                        if (inst != null)
677                            return new ServerString(inst, targetName);
678
679                    } else if (serverName != null) {
680                        Server server = getServer(serverName);
681                        if (server != null) {
682                            ServerInstance[] instances = server.getInstances();
683                            if (instances.length > 1)
684                                return new ServerString(instances[0]);
685                        }
686                    }
687                } catch (Exception e) {
688                    Logger.getLogger("global").log(Level.INFO, null, e);
689                }
690                return null;
691            }
692
693            static private Properties readProperties(File propFile) {
694                Properties prop = new Properties();
695                InputStream is = null;
696                try {
697                    if (propFile != null && propFile.exists()) {
698                        is = new FileInputStream(propFile);
699                        prop.load(is);
700                    }
701                } catch (IOException ioe) {
702                    Logger.getLogger("global").log(Level.INFO, ioe.toString());
703                } finally {
704                    try {
705                        if (is != null) {
706                            is.close();
707                        }
708                    } catch (IOException ex) {
709                        Logger.getLogger("global").log(Level.FINE, null, ex);
710                    }
711                }
712                return prop;
713            }
714
715            public ServerString getDefaultInstance() {
716                return getDefaultInstance(true);
717            }
718
719            public ServerString getDefaultInstance(boolean readFromFile) {
720                if (defaultInstance != null)
721                    return defaultInstance;
722
723                if (readFromFile) {
724                    defaultInstance = ServerStringConverter.readServerInstance(
725                            DIR_INSTALLED_SERVERS, FILE_DEFAULT_INSTANCE);
726
727                    if (defaultInstance == null) {
728                        defaultInstance = getInstallerDefaultPlugin();
729                    }
730
731                }
732
733                if (defaultInstance == null) {
734                    ServerInstance[] instances = getServerInstances();
735                    if (instances != null && instances.length > 0) {
736                        defaultInstance = new ServerString(instances[0]);
737                    }
738                }
739
740                setDefaultInstance(defaultInstance);
741                return defaultInstance;
742            }
743
744            public interface PluginListener extends EventListener {
745
746                public void serverAdded(Server name);
747
748                public void serverRemoved(Server name);
749
750            }
751
752            private static HashMap configNamesByType = null;
753            private static final Object[] allTypes = new Object[] {
754                    J2eeModule.EAR, J2eeModule.CLIENT, J2eeModule.CONN,
755                    J2eeModule.EJB, J2eeModule.WAR };
756
757            private void initConfigNamesByType() {
758                if (configNamesByType != null) {
759                    return;
760                }
761                configNamesByType = new HashMap();
762                for (int i = 0; i < allTypes.length; i++) {
763                    Set configNames = new HashSet();
764                    for (Iterator j = servers.values().iterator(); j.hasNext();) {
765                        Server s = (Server) j.next();
766                        String[] paths = s.getDeploymentPlanFiles(allTypes[i]);
767                        if (paths == null)
768                            continue;
769                        for (int k = 0; k < paths.length; k++) {
770                            File path = new File(paths[k]);
771                            configNames.add(path.getName());
772                        }
773                    }
774                    configNamesByType.put(allTypes[i], configNames);
775                }
776            }
777
778            public boolean isConfigFileName(String name, Object type) {
779                initConfigNamesByType();
780                Set configNames = (Set) configNamesByType.get(type);
781                return (configNames != null && configNames.contains(name));
782            }
783
784            public ServerInstance getInstanceOrDefault(String uri) {
785                ServerInstance instance = getServerInstance(uri);
786                if (instance == null) {
787                    instance = getDefaultInstance().getServerInstance();
788                }
789                if (instance != null)
790                    return instance;
791                throw new RuntimeException(NbBundle.getMessage(
792                        ServerRegistry.class, "MSG_NoServerInstances", uri));
793            }
794
795            /** Return profiler if any is registered in the IDE, null otherwise. */
796            public static Profiler getProfiler() {
797                return (Profiler) Lookup.getDefault().lookup(Profiler.class);
798            }
799        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.