KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > core > jaxws > nodes > HandlerButtonListener


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 package org.netbeans.modules.websvc.core.jaxws.nodes;
20
21 import com.sun.source.tree.AnnotationTree;
22 import com.sun.source.tree.AnnotationTree;
23 import com.sun.source.tree.AssignmentTree;
24 import com.sun.source.tree.ClassTree;
25 import com.sun.source.tree.ExpressionTree;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.io.BufferedReader JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.InputStreamReader JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.List JavaDoc;
35 import javax.lang.model.element.AnnotationMirror;
36 import javax.lang.model.element.TypeElement;
37 import javax.swing.DefaultListModel JavaDoc;
38 import javax.swing.ListModel JavaDoc;
39 import org.netbeans.api.java.source.CancellableTask;
40 import org.netbeans.api.java.source.JavaSource;
41 import org.netbeans.api.java.source.JavaSource.Phase;
42 import org.netbeans.api.java.source.TreeMaker;
43 import org.netbeans.api.java.source.WorkingCopy;
44 import org.netbeans.modules.j2ee.common.source.GenerationUtils;
45 import org.netbeans.modules.websvc.api.jaxws.project.WSUtils;
46 import org.netbeans.modules.websvc.core.webservices.ui.panels.MessageHandlerPanel;
47 import org.netbeans.modules.websvc.api.jaxws.project.config.Handler;
48 import org.netbeans.modules.websvc.api.jaxws.project.config.HandlerChain;
49 import org.netbeans.modules.websvc.api.jaxws.project.config.HandlerChains;
50 import org.netbeans.modules.websvc.api.jaxws.project.config.HandlerChainsProvider;
51 import org.netbeans.modules.websvc.api.jaxws.project.config.Service;
52 import org.openide.ErrorManager;
53 import org.openide.NotifyDescriptor;
54 import org.openide.filesystems.FileLock;
55 import org.openide.filesystems.FileObject;
56 import org.openide.filesystems.FileUtil;
57
58 /**
59  *
60  * @author Roderico Cruz, Milan Kuchtiak
61  */

62 public class HandlerButtonListener implements ActionListener JavaDoc{
63     MessageHandlerPanel panel;
64     HandlerChains handlerChains;
65     HandlerChain chain;
66     FileObject handlerFO;
67     //JavaClass implBeanClass;
68
FileObject implBeanClass;
69     Service service;
70     boolean isNew;
71     
72     /**
73      * If there is no HandlerChain annotation, handlerChains and handlerFO
74      * will both be null;
75      */

76     public HandlerButtonListener(MessageHandlerPanel panel,
77             HandlerChains handlerChains, FileObject handlerFO,
78             FileObject implBeanClass, Service service,
79             boolean isNew){
80         this.panel = panel;
81         this.handlerChains = handlerChains;
82         this.handlerFO = handlerFO;
83         this.implBeanClass = implBeanClass;
84         this.service = service;
85         this.isNew = isNew;
86     }
87     public void actionPerformed(ActionEvent JavaDoc evt) {
88         if(evt.getSource() == NotifyDescriptor.OK_OPTION) {
89             if (isNew) {
90                 //add annotation
91
String JavaDoc servicehandlerFileName = service.getName() + "_handler"; //NOI18N
92
FileObject parent = implBeanClass.getParent();
93                 final String JavaDoc handlerFileName = FileUtil.findFreeFileName(parent, servicehandlerFileName, "xml");
94                 CancellableTask<WorkingCopy> modificationTask = new CancellableTask<WorkingCopy>() {
95                     public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
96                         workingCopy.toPhase(Phase.RESOLVED);
97                         GenerationUtils genUtils = GenerationUtils.newInstance(workingCopy);
98                         if (genUtils!=null) {
99                             TreeMaker make = workingCopy.getTreeMaker();
100                             TypeElement chainElement = workingCopy.getElements().getTypeElement("javax.jws.HandlerChain"); //NOI18N
101
List JavaDoc<ExpressionTree> attrs = new ArrayList JavaDoc<ExpressionTree>();
102                             AssignmentTree attr1 = make.Assignment(make.Identifier("file"), make.Literal(handlerFileName + ".xml"));
103                             attrs.add(attr1);
104                             AnnotationTree chainAnnotation = make.Annotation(
105                                     make.QualIdent(chainElement),
106                                     attrs
107                             );
108                             ClassTree javaClass = genUtils.getClassTree();
109                             ClassTree modifiedClass = genUtils.addAnnotation(javaClass, chainAnnotation);
110                             workingCopy.rewrite(javaClass, modifiedClass);
111                         }
112                     }
113                     public void cancel() {}
114                 };
115                 JavaSource targetSource = JavaSource.forFileObject(implBeanClass);
116                 try {
117                     targetSource.runModificationTask(modificationTask).commit();
118                 } catch(IOException JavaDoc exp) {
119                         ErrorManager.getDefault().notify(exp);
120                 }
121                 
122                 handlerFO = parent.getFileObject(handlerFileName, "xml");
123                 if(handlerFO == null) {
124                     //create handler file
125
try {
126                         WSUtils.retrieveHandlerConfigFromResource(parent,handlerFileName + ".xml");
127                         handlerFO = parent.getFileObject(handlerFileName, "xml");
128                     }catch(Exception JavaDoc exp){
129                         ErrorManager.getDefault().notify(exp);
130                     }
131                 }
132                 //initialize handlerChains
133
try{
134                     handlerChains =
135                             HandlerChainsProvider.getDefault().getHandlerChains(handlerFO);
136                 }catch(Exception JavaDoc e){
137                     ErrorManager.getDefault().notify(e);
138                     return; //TODO handle this
139
}
140             }
141             DefaultListModel JavaDoc listModel = panel.getListModel();
142             chain = handlerChains.getHandlerChains()[0];
143             //add new handlers
144
for(int i = 0; i < listModel.getSize(); i++){
145                 String JavaDoc className = (String JavaDoc)listModel.getElementAt(i);
146                 if(isNewHandler(className, chain)){
147                     chain.addHandler(className, className);
148                 }
149             }
150             //remove handlers that have been deleted
151
Handler[] handlers = chain.getHandlers();
152             for(int j = 0; j < handlers.length; j++){
153                 Handler handler = handlers[j];
154                 String JavaDoc clsName = handler.getHandlerClass();
155                 if(!isInModel(clsName, listModel)){
156                     chain.removeHandler(clsName);
157                 }
158             }
159             //if handler chain has no handlers, delete the annotation
160
// and delete the handler xml file
161
FileLock lock = null;
162             OutputStream JavaDoc out = null;
163             if(chain.getHandlers().length == 0) {
164                 
165                 CancellableTask<WorkingCopy> modificationTask = new CancellableTask<WorkingCopy>() {
166                     public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
167                         workingCopy.toPhase(Phase.RESOLVED);
168                         GenerationUtils genUtils = GenerationUtils.newInstance(workingCopy);
169                         if (genUtils!=null) {
170                             TreeMaker make = workingCopy.getTreeMaker();
171                             AnnotationMirror chainAnnotation = JaxWsNode.getAnnotation(workingCopy, genUtils, "javax.jws.HandlerChain"); //NOI18N
172
if (chainAnnotation!=null) {
173                                 ClassTree classTree = genUtils.getClassTree();
174                                 AnnotationTree anotTree = (AnnotationTree)workingCopy.getTrees().getTree(genUtils.getTypeElement(),chainAnnotation);
175                                 ClassTree modifiedClass = make.Class(
176                                     make.removeModifiersAnnotation(classTree.getModifiers(), anotTree),
177                                     classTree.getSimpleName(),
178                                     classTree.getTypeParameters(),
179                                     classTree.getExtendsClause(),
180                                     (List JavaDoc<ExpressionTree>)classTree.getImplementsClause(),
181                                     classTree.getMembers());
182                                 workingCopy.rewrite(classTree, modifiedClass);
183                             }
184                         }
185                     }
186                     public void cancel() {}
187                 };
188                 JavaSource targetSource = JavaSource.forFileObject(implBeanClass);
189                 try {
190                     targetSource.runModificationTask(modificationTask).commit();
191                 } catch(IOException JavaDoc exp) {
192                         ErrorManager.getDefault().notify(exp);
193                 }
194                 
195                 //delete the handler xml file
196
try{
197                     lock = handlerFO.lock();
198                     handlerFO.delete(lock);
199                 }catch(Exception JavaDoc e){
200                     ErrorManager.getDefault().notify(e);
201                 } finally{
202                     if(lock != null){
203                         lock.releaseLock();
204                     }
205                 }
206             } else{
207                 try{
208                     lock = handlerFO.lock();
209                     out = handlerFO.getOutputStream(lock);
210                     handlerChains.write(out);
211                 }catch(Exception JavaDoc e){
212                     ErrorManager.getDefault().notify(e);
213                 }finally{
214                     if(lock != null){
215                         lock.releaseLock();
216                     }
217                     if (out != null){
218                         try{
219                             out.close();
220                         } catch(IOException JavaDoc ioe){
221                             ErrorManager.getDefault().notify(ioe);
222                         }
223                     }
224                 }
225             }
226             
227         }
228     }
229     
230     private boolean isInModel(String JavaDoc className, ListModel JavaDoc model) {
231         for(int i = 0; i < model.getSize(); i++){
232             String JavaDoc cls = (String JavaDoc)model.getElementAt(i);
233             if(className.equals(cls)){
234                 return true;
235             }
236         }
237         return false;
238     }
239     
240     private boolean isNewHandler(String JavaDoc className, HandlerChain handlerChain){
241         if(handlerChain != null){
242             Handler[] handlers = handlerChain.getHandlers();
243             for(int i = 0; i < handlers.length; i++){
244                 if(handlers[i].getHandlerClass().equals(className)){
245                     return false;
246                 }
247             }
248         }
249         return true;
250     }
251     
252     private static String JavaDoc readResource(InputStream JavaDoc is) throws IOException JavaDoc {
253         // read the config from resource first
254
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
255         String JavaDoc lineSep = System.getProperty("line.separator");//NOI18N
256
BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
257         String JavaDoc line = br.readLine();
258         while (line != null) {
259             sb.append(line);
260             sb.append(lineSep);
261             line = br.readLine();
262         }
263         br.close();
264         return sb.toString();
265     }
266 }
267
Popular Tags