1 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 ; 27 import java.awt.event.ActionListener ; 28 import java.io.BufferedReader ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.InputStreamReader ; 32 import java.io.OutputStream ; 33 import java.util.ArrayList ; 34 import java.util.List ; 35 import javax.lang.model.element.AnnotationMirror; 36 import javax.lang.model.element.TypeElement; 37 import javax.swing.DefaultListModel ; 38 import javax.swing.ListModel ; 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 62 public class HandlerButtonListener implements ActionListener { 63 MessageHandlerPanel panel; 64 HandlerChains handlerChains; 65 HandlerChain chain; 66 FileObject handlerFO; 67 FileObject implBeanClass; 69 Service service; 70 boolean isNew; 71 72 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 evt) { 88 if(evt.getSource() == NotifyDescriptor.OK_OPTION) { 89 if (isNew) { 90 String servicehandlerFileName = service.getName() + "_handler"; FileObject parent = implBeanClass.getParent(); 93 final String handlerFileName = FileUtil.findFreeFileName(parent, servicehandlerFileName, "xml"); 94 CancellableTask<WorkingCopy> modificationTask = new CancellableTask<WorkingCopy>() { 95 public void run(WorkingCopy workingCopy) throws IOException { 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"); List <ExpressionTree> attrs = new ArrayList <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 exp) { 119 ErrorManager.getDefault().notify(exp); 120 } 121 122 handlerFO = parent.getFileObject(handlerFileName, "xml"); 123 if(handlerFO == null) { 124 try { 126 WSUtils.retrieveHandlerConfigFromResource(parent,handlerFileName + ".xml"); 127 handlerFO = parent.getFileObject(handlerFileName, "xml"); 128 }catch(Exception exp){ 129 ErrorManager.getDefault().notify(exp); 130 } 131 } 132 try{ 134 handlerChains = 135 HandlerChainsProvider.getDefault().getHandlerChains(handlerFO); 136 }catch(Exception e){ 137 ErrorManager.getDefault().notify(e); 138 return; } 140 } 141 DefaultListModel listModel = panel.getListModel(); 142 chain = handlerChains.getHandlerChains()[0]; 143 for(int i = 0; i < listModel.getSize(); i++){ 145 String className = (String )listModel.getElementAt(i); 146 if(isNewHandler(className, chain)){ 147 chain.addHandler(className, className); 148 } 149 } 150 Handler[] handlers = chain.getHandlers(); 152 for(int j = 0; j < handlers.length; j++){ 153 Handler handler = handlers[j]; 154 String clsName = handler.getHandlerClass(); 155 if(!isInModel(clsName, listModel)){ 156 chain.removeHandler(clsName); 157 } 158 } 159 FileLock lock = null; 162 OutputStream out = null; 163 if(chain.getHandlers().length == 0) { 164 165 CancellableTask<WorkingCopy> modificationTask = new CancellableTask<WorkingCopy>() { 166 public void run(WorkingCopy workingCopy) throws IOException { 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"); 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 <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 exp) { 192 ErrorManager.getDefault().notify(exp); 193 } 194 195 try{ 197 lock = handlerFO.lock(); 198 handlerFO.delete(lock); 199 }catch(Exception 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 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 ioe){ 221 ErrorManager.getDefault().notify(ioe); 222 } 223 } 224 } 225 } 226 227 } 228 } 229 230 private boolean isInModel(String className, ListModel model) { 231 for(int i = 0; i < model.getSize(); i++){ 232 String cls = (String )model.getElementAt(i); 233 if(className.equals(cls)){ 234 return true; 235 } 236 } 237 return false; 238 } 239 240 private boolean isNewHandler(String 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 readResource(InputStream is) throws IOException { 253 StringBuffer sb = new StringBuffer (); 255 String lineSep = System.getProperty("line.separator"); BufferedReader br = new BufferedReader (new InputStreamReader (is)); 257 String 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 |