KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > oc4j > nodes > OC4JItemNode


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 package org.netbeans.modules.j2ee.oc4j.nodes;
21
22 import java.awt.Image JavaDoc;
23 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
24 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
25 import javax.management.MBeanServerConnection JavaDoc;
26 import org.netbeans.api.progress.ProgressHandle;
27 import org.netbeans.api.progress.ProgressHandleFactory;
28 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
29 import org.netbeans.modules.j2ee.deployment.plugins.api.UISupport;
30 import org.netbeans.modules.j2ee.deployment.plugins.api.UISupport.ServerIcon;
31 import org.netbeans.modules.j2ee.oc4j.OC4JDeploymentManager;
32 import org.netbeans.modules.j2ee.oc4j.config.OC4JDatasourceManager;
33 import org.netbeans.modules.j2ee.oc4j.nodes.actions.OpenURLAction;
34 import org.netbeans.modules.j2ee.oc4j.nodes.actions.OpenURLActionCookie;
35 import org.netbeans.modules.j2ee.oc4j.nodes.actions.RefreshModulesAction;
36 import org.netbeans.modules.j2ee.oc4j.nodes.actions.RefreshModulesCookie;
37 import org.netbeans.modules.j2ee.oc4j.nodes.actions.Refreshable;
38 import org.netbeans.modules.j2ee.oc4j.nodes.actions.UndeployModuleAction;
39 import org.netbeans.modules.j2ee.oc4j.nodes.actions.UndeployModuleCookie;
40 import org.netbeans.modules.j2ee.oc4j.util.OC4JPluginProperties;
41 import org.netbeans.modules.j2ee.oc4j.util.OC4JPluginUtils;
42 import org.openide.filesystems.Repository;
43 import org.openide.loaders.DataFolder;
44 import org.openide.nodes.AbstractNode;
45 import org.openide.nodes.Children;
46 import org.openide.nodes.Node;
47 import org.openide.util.Lookup;
48 import org.openide.util.NbBundle;
49 import org.openide.util.RequestProcessor;
50 import org.openide.util.RequestProcessor.Task;
51 import org.openide.util.Utilities;
52 import org.openide.util.actions.SystemAction;
53
54 /**
55  * Universal extensible node.
56  *
57  * @author Michal Mocnak
58  */

59 public class OC4JItemNode extends AbstractNode {
60     
61     private ItemType type;
62     
63     private static final String JavaDoc HTTP_HEADER = "http://";
64     
65     private static final int TIMEOUT = 30000;
66     
67     private OC4JItemNode(Children children, final TargetModuleID JavaDoc module, final Lookup lookup, final ItemType type) {
68         super(children);
69         this.type = type;
70         
71         if(type.equals(ItemType.J2EE_APPLICATION_FOLDER) ||
72                 type.equals(ItemType.REFRESHABLE_FOLDER)) {
73             getCookieSet().add(new RefreshModulesCookie() {
74                 public void refresh() {
75                     Children children = getChildren();
76                     if(children instanceof Refreshable)
77                         ((Refreshable)children).updateKeys();
78                 }
79             });
80         } else if(type.equals(ItemType.J2EE_APPLICATION)) {
81             getCookieSet().add(new OpenURLActionCookie() {
82                 public String JavaDoc getWebURL() {
83                     if(module == null || lookup == null)
84                         return null;
85                     
86                     try {
87                         OC4JDeploymentManager dm = lookup.lookup(OC4JDeploymentManager.class);
88                         String JavaDoc app = null;
89                         
90                         if(module.getWebURL() != null)
91                             app = module.getWebURL();
92                         
93                         for(TargetModuleID JavaDoc id:module.getChildTargetModuleID()) {
94                             if(id.getWebURL() != null) {
95                                 app = id.getWebURL();
96                                 break;
97                             }
98                         }
99                         
100                         InstanceProperties ip = dm.getInstanceProperties();
101                         
102                         String JavaDoc host = ip.getProperty(OC4JPluginProperties.PROPERTY_HOST);
103                         String JavaDoc httpPort = ip.getProperty(InstanceProperties.HTTP_PORT_NUMBER);
104                         
105                         if(app == null || host == null || httpPort == null)
106                             return null;
107                         
108                         return HTTP_HEADER + host + ":" + httpPort + app;
109                     } catch (Throwable JavaDoc t) {
110                         return null;
111                     }
112                 }
113             });
114             getCookieSet().add(new UndeployModuleCookie() {
115                 private boolean isRunning = false;
116                 
117                 public Task undeploy() {
118                     final OC4JDeploymentManager dm = lookup.lookup(OC4JDeploymentManager.class);
119                     final ProgressHandle handle = ProgressHandleFactory.createHandle(NbBundle.getMessage(OC4JItemNode.class,
120                             "LBL_UndeployProgress", OC4JPluginUtils.getName(module)));
121                     
122                     Runnable JavaDoc r = new Runnable JavaDoc() {
123                         public void run() {
124                             isRunning = true;
125                             
126                             // Save the current time so that we can deduct that the undeploy
127
// failed due to timeout
128
long start = System.currentTimeMillis();
129                             
130                             ProgressObject JavaDoc o = dm.undeploy(new TargetModuleID JavaDoc[] {module});
131                             
132                             while(!o.getDeploymentStatus().isCompleted() && System.currentTimeMillis() - start < TIMEOUT) {
133                                 try {
134                                     Thread.sleep(2000);
135                                 } catch(InterruptedException JavaDoc ex) {
136                                     // Nothing to do
137
}
138                             }
139                             
140                             handle.finish();
141                             isRunning = false;
142                         }
143                     };
144                     
145                     handle.start();
146                     return RequestProcessor.getDefault().post(r);
147                 }
148                 
149                 public synchronized boolean isRunning() {
150                     return isRunning;
151                 }
152             });
153         } else if (type.equals(ItemType.JDBC_NATIVE_DATASOURCES) ||
154                 type.equals(ItemType.JDBC_MANAGED_DATASOURCES) ||
155                 type.equals(ItemType.CONNECTION_POOLS)) {
156             getCookieSet().add(new UndeployModuleCookie() {
157                 private boolean isRunning = false;
158                 
159                 public Task undeploy() {
160                     final OC4JDeploymentManager dm = lookup.lookup(OC4JDeploymentManager.class);
161                     final ProgressHandle handle = ProgressHandleFactory.createHandle(NbBundle.getMessage(OC4JItemNode.class,
162                             "LBL_UndeployProgress", getDisplayName()));
163                     
164                     Runnable JavaDoc r = new Runnable JavaDoc() {
165                         public void run() {
166                             isRunning = true;
167                             
168                             OC4JDatasourceManager dsManager = new OC4JDatasourceManager(dm);
169                             
170                             // Undeploying
171
if(type.equals(ItemType.JDBC_NATIVE_DATASOURCES)) {
172                                 dsManager.undeployNativeDataSource(getDisplayName());
173                             } else if (type.equals(ItemType.JDBC_MANAGED_DATASOURCES)) {
174                                 dsManager.undeployManagedDataSource(getDisplayName());
175                             } else if (type.equals(ItemType.CONNECTION_POOLS)) {
176                                 dsManager.undeployConnectionPool(getDisplayName());
177                             }
178                             
179                             handle.finish();
180                             isRunning = false;
181                         }
182                     };
183                     
184                     handle.start();
185                     return RequestProcessor.getDefault().post(r);
186                 }
187                 
188                 public synchronized boolean isRunning() {
189                     return isRunning;
190                 }
191             });
192         }
193     }
194     
195     public OC4JItemNode(Lookup lookup, TargetModuleID JavaDoc module, ItemType type) {
196         this(Children.LEAF, module, lookup, type);
197         setDisplayName(OC4JPluginUtils.getName(module));
198     }
199     
200     public OC4JItemNode(Lookup lookup, Children children, String JavaDoc name, ItemType type) {
201         this(children, null, lookup, type);
202         setDisplayName(name);
203     }
204     
205     public Image JavaDoc getIcon(int type) {
206         if(this.type.equals(ItemType.J2EE_APPLICATION_FOLDER)) {
207             return UISupport.getIcon(ServerIcon.EAR_FOLDER);
208         } else if(this.type.equals(ItemType.J2EE_APPLICATION) ||
209                 this.type.equals(ItemType.J2EE_APPLICATION_SYSTEM)) {
210             return UISupport.getIcon(ServerIcon.EAR_ARCHIVE);
211         } else if(this.type.equals(ItemType.JDBC_MANAGED_DATASOURCES) ||
212                 this.type.equals(ItemType.JDBC_NATIVE_DATASOURCES)) {
213             return Utilities.loadImage(JDBC_RESOURCE_ICON);
214         } else if(this.type.equals(ItemType.CONNECTION_POOLS)) {
215             return Utilities.loadImage(CONNECTION_POOL_ICON);
216         } else {
217             return getIconDelegate().getIcon(type);
218         }
219     }
220     
221     public Image JavaDoc getOpenedIcon(int type) {
222         if(this.type.equals(ItemType.J2EE_APPLICATION_FOLDER)) {
223             return UISupport.getIcon(ServerIcon.EAR_OPENED_FOLDER);
224         } else if(this.type.equals(ItemType.J2EE_APPLICATION) ||
225                 this.type.equals(ItemType.J2EE_APPLICATION_SYSTEM) ||
226                 this.type.equals(ItemType.JDBC_MANAGED_DATASOURCES) ||
227                 this.type.equals(ItemType.JDBC_NATIVE_DATASOURCES) ||
228                 this.type.equals(ItemType.CONNECTION_POOLS)) {
229             return getIcon(type);
230         } else {
231             return getIconDelegate().getOpenedIcon(type);
232         }
233     }
234     
235     private Node getIconDelegate() {
236         return DataFolder.findFolder(Repository.getDefault().getDefaultFileSystem().getRoot()).getNodeDelegate();
237     }
238     
239     public javax.swing.Action JavaDoc[] getActions(boolean context) {
240         SystemAction[] actions = new SystemAction[] {};
241         
242         if(type.equals(ItemType.J2EE_APPLICATION_FOLDER)
243                 || type.equals(ItemType.REFRESHABLE_FOLDER)) {
244             actions = new SystemAction[] {
245                 SystemAction.get(RefreshModulesAction.class)
246             };
247         } else if(type.equals(ItemType.J2EE_APPLICATION)) {
248             actions = new SystemAction[] {
249                 SystemAction.get(OpenURLAction.class),
250                 SystemAction.get(UndeployModuleAction.class)
251             };
252         } else if(type.equals(ItemType.JDBC_NATIVE_DATASOURCES) ||
253                 type.equals(ItemType.JDBC_MANAGED_DATASOURCES) ||
254                 type.equals(ItemType.CONNECTION_POOLS)) {
255             actions = new SystemAction[] {
256                 SystemAction.get(UndeployModuleAction.class)
257             };
258         }
259         
260         return actions;
261     }
262     
263     /* Creates and returns the instance of the node
264      * representing the status 'WAIT' of the node.
265      * It is used when it spent more time to create elements hierarchy.
266      * @return the wait node.
267      */

268     public static Node createWaitNode() {
269         AbstractNode n = new AbstractNode(Children.LEAF);
270         n.setName(NbBundle.getMessage(OC4JItemNode.class, "LBL_WaitNode_DisplayName")); //NOI18N
271
n.setIconBaseWithExtension("org/openide/src/resources/wait.gif"); // NOI18N
272
return n;
273     }
274     
275     private static final String JavaDoc JDBC_RESOURCE_ICON = "org/netbeans/modules/j2ee/oc4j/resources/JDBCResource.gif";
276     private static final String JavaDoc CONNECTION_POOL_ICON = "org/netbeans/modules/j2ee/oc4j/resources/ConnectionPool.gif";
277     
278     public static class ItemType {
279         
280         public static ItemType J2EE_APPLICATION_FOLDER = new ItemType();
281         public static ItemType J2EE_APPLICATION = new ItemType();
282         public static ItemType J2EE_APPLICATION_SYSTEM = new ItemType();
283         public static ItemType REFRESHABLE_FOLDER = new ItemType();
284         public static ItemType JDBC_MANAGED_DATASOURCES = new ItemType();
285         public static ItemType JDBC_NATIVE_DATASOURCES = new ItemType();
286         public static ItemType CONNECTION_POOLS = new ItemType();
287         
288         private ItemType() {}
289     }
290 }
Popular Tags