KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ws7 > nodes > WS70TargetNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * WS70TargetNode.java
22  */

23
24 package org.netbeans.modules.j2ee.sun.ws7.nodes;
25
26 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
27 import javax.enterprise.deploy.spi.Target JavaDoc;
28
29 import org.openide.nodes.AbstractNode;
30 import org.openide.nodes.Node;
31 import org.openide.nodes.Children;
32 import org.openide.util.Lookup;
33
34 import java.io.File JavaDoc;
35 import org.openide.windows.InputOutput;
36 import java.util.List JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Collections JavaDoc;
40 import java.lang.reflect.Method JavaDoc;
41 import javax.swing.Action JavaDoc;
42 import java.awt.Image JavaDoc;
43 import org.openide.util.Utilities;
44 import org.openide.util.NbBundle;
45 import org.openide.ErrorManager;
46
47 import org.netbeans.modules.j2ee.sun.ws7.j2ee.ResourceType;
48 import org.netbeans.modules.j2ee.sun.ws7.dm.WS70SunDeploymentManager;
49 import org.openide.util.actions.SystemAction;
50 import org.openide.actions.PropertiesAction;
51 import org.netbeans.modules.j2ee.sun.ws7.nodes.actions.StartStopServerAction;
52 import org.netbeans.modules.j2ee.sun.ws7.nodes.actions.ViewTargetServerLogAction;
53 import org.netbeans.modules.j2ee.sun.ws7.nodes.actions.EditServerXmlAction;
54 import org.netbeans.modules.j2ee.sun.ws7.j2ee.WS70LogViewer;
55
56 import org.openide.filesystems.FileObject;
57 import org.openide.filesystems.FileUtil;
58 import org.openide.filesystems.FileChangeAdapter;
59 import org.openide.filesystems.FileChangeListener;
60 import org.openide.filesystems.FileEvent;
61 import org.openide.loaders.DataObject;
62 import org.openide.loaders.DataObjectNotFoundException;
63 import org.openide.cookies.EditorCookie;
64
65 import org.openide.windows.IOProvider;
66 import org.openide.windows.OutputWriter;
67
68 /**
69  *
70  * @author Mukesh Garg
71  */

72 public class WS70TargetNode extends AbstractNode implements Node.Cookie{
73     private WS70SunDeploymentManager manager;
74     private Target JavaDoc target;
75     private Lookup looup;
76     private String JavaDoc configName;
77     private boolean isConfigChanged = false;
78     private static Map JavaDoc serverXmlListeners =
79             Collections.synchronizedMap((Map JavaDoc)new HashMap JavaDoc(2,1));
80     
81     /** Creates a new instance of WS70TargetNode */
82     public WS70TargetNode(Lookup lookup){
83         super(new Children.Array());
84         this.looup = lookup;
85         target = (Target JavaDoc)lookup.lookup(Target JavaDoc.class);
86         setName(target.getName());
87         try{
88             Method JavaDoc getConfigName = target.getClass().getDeclaredMethod("getConfigName", new Class JavaDoc[]{});
89             configName = (String JavaDoc)getConfigName.invoke(target, new Object JavaDoc[]{});
90
91         }catch(Exception JavaDoc ex){
92             ex.printStackTrace();
93         }
94         this.manager = (WS70SunDeploymentManager)lookup.lookup(DeploymentManager JavaDoc.class);
95
96         List JavaDoc jvmOptions = ((WS70SunDeploymentManager)manager).getJVMOptions(configName, Boolean.valueOf(false), null);
97         Map JavaDoc jvmProps = ((WS70SunDeploymentManager)manager).getJVMProps(configName);
98
99         WS70JVMManagedObject jvm = new WS70JVMManagedObject(this.manager, configName,
100                                                             (HashMap JavaDoc)jvmProps, jvmOptions);
101
102
103         setIconBaseWithExtension("org/netbeans/modules/j2ee/sun/ws7/resources/ConfigIcon.gif");
104         getCookieSet().add(this);
105         getChildren().add(new Node[] {new WS70WebModulesRootNode(lookup),
106                                       new WS70ResourcesRootNode(lookup, ResourceType.JDBC),
107                                       new WS70ResourcesRootNode(lookup, ResourceType.JNDI),
108                                       new WS70ResourcesRootNode(lookup, ResourceType.CUSTOM),
109                                       new WS70ResourcesRootNode(lookup, ResourceType.MAIL),
110                                       new WS70JVMNode(jvm)});
111         setMyDisplayName();
112         
113     }
114     public Action JavaDoc getPreferredAction() {
115         return SystemAction.get(PropertiesAction.class);
116     }
117     public Action JavaDoc[] getActions(boolean context) {
118         return new SystemAction[] {
119             SystemAction.get(StartStopServerAction.class),
120             null,
121             SystemAction.get(EditServerXmlAction.class),
122             SystemAction.get(ViewTargetServerLogAction.class),
123         };
124     }
125     
126     public boolean isRunning(){
127         return manager.isRunning(configName);
128     }
129     public boolean isLocalServer(){
130         return manager.isLocalServer();
131     }
132     public void startTarget(){
133         try{
134             manager.startServer(configName);
135         }catch(Exception JavaDoc ex){
136         }
137         // If Webserver is a remote instance, can't see the logs
138
//Issue# 75329
139
if(manager.isLocalServer()){
140             invokeLogViewer();
141         }
142     }
143     public void stopTarget(){
144         try{
145             manager.stopServer(configName);
146         }catch(Exception JavaDoc ex){
147         }
148     }
149     public void invokeLogViewer(){
150         String JavaDoc location = manager.getServerLocation();
151         location = location+File.separator+"https-"+configName+
152                 File.separator+"logs"+File.separator+"errors";
153         String JavaDoc logName = NbBundle.getMessage(WS70TargetNode.class, "LBL_WS70_MANAGER_NODE_NAME")+
154                 "--"+configName;
155         WS70LogViewer logViewer = new WS70LogViewer(new File JavaDoc(location));
156         InputOutput io = IOProvider.getDefault().getIO(logName, false);
157         try{
158             logViewer.showLogViewer(io);
159         }catch(Exception JavaDoc ex){
160             ErrorManager.getDefault().notify(ErrorManager.WARNING, ex);
161         }
162         
163     }
164     public void showServerXml(){
165         String JavaDoc location = manager.getServerLocation()+File.separator+
166                           "admin-server"+File.separator+"config-store"+ //NOI18N
167
File.separator+configName+ File.separator+//NOI18N
168
"config";//NOI18N
169
File JavaDoc serverXmlFile = new File JavaDoc(location, "server.xml");//NOI18N
170
FileObject fileObject = FileUtil.toFileObject(serverXmlFile);
171         if (fileObject == null) {
172             OutputWriter writer = IOProvider.getDefault().getStdOut();
173             writer.println(NbBundle.getMessage(WS70TargetNode.class,
174                               "ERR_Server_XML_not_found", configName));
175             return;
176         }
177         DataObject dataObject = null;
178         try {
179             dataObject = DataObject.find(fileObject);
180         } catch(DataObjectNotFoundException ex) {
181             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
182         }
183         if (dataObject != null) {
184             EditorCookie editorCookie = (EditorCookie)dataObject.getCookie(EditorCookie.class);
185             if (editorCookie != null) {
186                 editorCookie.open();
187                 if(serverXmlListeners.get(location)==null){
188                     ServerFileChangeListener listener = new ServerFileChangeListener(editorCookie);
189                     fileObject.addFileChangeListener(listener);
190                     serverXmlListeners.put(location, listener);
191                 }
192             } else {
193                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "Cannot find EditorCookie."); // NOI18N
194
}
195         }
196     }
197     private void setMyDisplayName(){
198         this.setDisplayName(target.getName());
199     }
200     public void fireChangeIcon(){
201         isConfigChanged = true;
202         this.fireIconChange();
203     }
204     
205     public Image JavaDoc getIcon(int type){
206         Image JavaDoc img1 = this.getOpenedIcon(type);
207         if(!isConfigChanged){
208             return img1;
209         }else{
210             Image JavaDoc img2 = Utilities.loadImage("org/netbeans/modules/j2ee/sun/ws7/resources/ConfigChanged.gif");
211             Image JavaDoc img3 = Utilities.mergeImages(img1, img2, 15, 8);
212             return img3;
213         }
214
215     }
216  private class ServerFileChangeListener extends FileChangeAdapter{
217      private EditorCookie cookie;
218      public ServerFileChangeListener(EditorCookie e){
219          cookie = e;
220      }
221      public void fileChanged(FileEvent fe){
222          if(!cookie.isModified()){
223             return; // server.xml was modified outside of IDE
224
}
225          OutputWriter writer = IOProvider.getDefault().getStdOut();
226          
227          try {
228              FileObject obj = fe.getFile();
229              writer.println(NbBundle.getMessage(WS70TargetNode.class,
230                               "MSG_Deploying_Config", configName));
231              manager.deployAndReconfig(configName);
232              writer.println(NbBundle.getMessage(WS70TargetNode.class,
233                               "MSG_Config_Deployed", configName));
234          } catch(Exception JavaDoc ex){
235              writer.println(NbBundle.getMessage(WS70TargetNode.class,
236                               "ERR_Config_deployed_failed", ex.getMessage()));
237          }
238      }
239  }
240 }
241
Popular Tags