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: package org.netbeans.modules.websvc.core.jaxws.nodes;
042:
043: import java.awt.event.ActionEvent;
044: import java.awt.event.ActionListener;
045: import java.io.BufferedReader;
046: import java.io.BufferedWriter;
047: import java.io.File;
048: import java.io.IOException;
049: import java.io.InputStream;
050: import java.io.InputStreamReader;
051: import java.io.OutputStream;
052: import java.io.OutputStreamWriter;
053: import java.net.URI;
054: import java.util.Collection;
055: import javax.swing.table.TableModel;
056: import org.netbeans.modules.websvc.api.jaxws.client.JAXWSClientSupport;
057: import org.netbeans.modules.websvc.core.jaxws.bindings.model.BindingsComponentFactory;
058: import org.netbeans.modules.websvc.core.jaxws.bindings.model.BindingsHandler;
059: import org.netbeans.modules.websvc.core.jaxws.bindings.model.BindingsHandlerChain;
060: import org.netbeans.modules.websvc.core.jaxws.bindings.model.BindingsHandlerChains;
061: import org.netbeans.modules.websvc.core.jaxws.bindings.model.BindingsHandlerClass;
062: import org.netbeans.modules.websvc.core.jaxws.bindings.model.BindingsModel;
063: import org.netbeans.modules.websvc.core.jaxws.bindings.model.BindingsModelFactory;
064: import org.netbeans.modules.websvc.core.jaxws.bindings.model.DefinitionsBindings;
065: import org.netbeans.modules.websvc.core.jaxws.bindings.model.GlobalBindings;
066: import org.netbeans.modules.websvc.core.webservices.ui.panels.MessageHandlerPanel;
067: import org.netbeans.modules.websvc.api.jaxws.project.config.Binding;
068: import org.netbeans.modules.websvc.api.jaxws.project.config.Client;
069: import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel;
070: import org.netbeans.modules.websvc.core.jaxws.bindings.model.BindingsHandlerName;
071: import org.netbeans.modules.xml.xam.ModelSource;
072: import org.netbeans.modules.xml.retriever.catalog.Utilities;
073: import org.openide.ErrorManager;
074: import org.openide.NotifyDescriptor;
075: import org.openide.cookies.SaveCookie;
076: import org.openide.filesystems.FileLock;
077: import org.openide.filesystems.FileObject;
078: import org.openide.filesystems.FileSystem;
079: import org.openide.filesystems.FileUtil;
080: import org.openide.filesystems.Repository;
081: import org.openide.loaders.DataObject;
082: import org.openide.nodes.Node;
083:
084: /**
085: *
086: * @author Roderico Cruz
087: */
088: public class ClientHandlerButtonListener implements ActionListener {
089:
090: private MessageHandlerPanel panel;
091: private BindingsModel bindingsModel;
092: private Client client;
093: private Node node;
094: private JaxWsModel jaxWsModel;
095: //private FileObject bindingHandlerFO;
096: private String bindingsHandlerFile;
097:
098: public ClientHandlerButtonListener(MessageHandlerPanel panel,
099: BindingsModel bindingsModel, Client client, Node node,
100: JaxWsModel jaxWsModel) {
101:
102: this .panel = panel;
103: this .bindingsModel = bindingsModel;
104: this .client = client;
105: this .node = node;
106: this .jaxWsModel = jaxWsModel;
107: }
108:
109: public void actionPerformed(ActionEvent evt) {
110:
111: if (evt.getSource() == NotifyDescriptor.OK_OPTION) {
112: if (!panel.isChanged()) {
113: return;
114: }
115: try {
116: FileObject srcRoot = (FileObject) node.getLookup()
117: .lookup(FileObject.class);
118: JAXWSClientSupport support = JAXWSClientSupport
119: .getJaxWsClientSupport(srcRoot);
120: final FileObject bindingsFolder = support
121: .getBindingsFolderForClient(node.getName(),
122: true);
123: Client client = (Client) node.getLookup().lookup(
124: Client.class);
125: assert client != null;
126: bindingsHandlerFile = client.getHandlerBindingFile();
127: if (bindingsHandlerFile == null) {
128: String baseBindingsHandlerFile = node.getName()
129: + "_handler";
130: bindingsHandlerFile = FileUtil.findFreeFileName(
131: bindingsFolder, baseBindingsHandlerFile,
132: "xml")
133: + ".xml";
134: client.setHandlerBindingFile(bindingsHandlerFile);
135: }
136: final FileObject bindingHandlerFO = FileUtil
137: .createData(bindingsFolder, bindingsHandlerFile);
138: //if bindingsModel is null, create it
139: if (bindingsModel == null) {
140: InputStream is = Repository
141: .getDefault()
142: .getDefaultFileSystem()
143: .findResource(
144: "jax-ws/default-binding-handler.xml")
145: .getInputStream();
146: final String bindingsContent = readResource(is); //NOI18N
147: is.close();
148:
149: bindingsFolder.getFileSystem().runAtomicAction(
150: new FileSystem.AtomicAction() {
151:
152: public void run() throws IOException {
153: //bindingHandlerFO =
154: // FileUtil.createData(bindingsFolder, bindingsHandlerFile);//NOI18N
155: BufferedWriter bw = null;
156: OutputStream os = null;
157: OutputStreamWriter osw = null;
158: FileLock lock = bindingHandlerFO
159: .lock();
160: try {
161: os = bindingHandlerFO
162: .getOutputStream(lock);
163: osw = new OutputStreamWriter(os);
164: bw = new BufferedWriter(osw);
165: bw.write(bindingsContent);
166: } finally {
167: try {
168: if (bw != null) {
169: bw.close();
170: }
171: if (os != null) {
172: os.close();
173: }
174: if (osw != null) {
175: osw.close();
176: }
177: } catch (IOException e) {
178: ErrorManager.getDefault()
179: .notify(e);
180: }
181:
182: if (lock != null) {
183: lock.releaseLock();
184: }
185: }
186: }
187: });
188:
189: //now load the model and add the entry
190: ModelSource ms = Utilities.getModelSource(
191: bindingHandlerFO, true);
192: bindingsModel = BindingsModelFactory.getDefault()
193: .getModel(ms);
194: //get the relative path of the wsdl
195: FileObject localWsdlFile = support
196: .getLocalWsdlFolderForClient(
197: client.getName(), false)
198: .getFileObject(client.getLocalWsdlFile());
199: File f = FileUtil.toFile(bindingHandlerFO);
200: String relativePath = Utilities.relativize(f
201: .toURI(), new URI(localWsdlFile.getURL()
202: .toExternalForm()));
203: GlobalBindings gb = bindingsModel
204: .getGlobalBindings();
205: try {
206: bindingsModel.startTransaction();
207: gb.setWsdlLocation(relativePath);
208: } finally {
209: bindingsModel.endTransaction(); //becomes locked here
210: }
211:
212: //hack
213: DataObject dobj = DataObject.find(bindingHandlerFO);
214: if (dobj.isModified()) {
215: SaveCookie saveCookie = dobj
216: .getCookie(SaveCookie.class);
217: saveCookie.save(); //becomes false here
218: }
219: //end hack
220:
221: }//end if bindingsModel == null
222:
223: //get handler chain
224: TableModel tableModel = panel.getHandlerTableModel();
225: GlobalBindings gb = bindingsModel.getGlobalBindings();
226: DefinitionsBindings db = gb.getDefinitionsBindings();
227: BindingsHandlerChains bhc = db.getHandlerChains();
228: BindingsHandlerChain chain = bhc.getHandlerChains()
229: .iterator().next();
230:
231: //refresh handlers
232: try {
233: bindingsModel.startTransaction();
234: Collection<BindingsHandler> handlers = chain
235: .getHandlers();
236: for (BindingsHandler handler : handlers) {
237: chain.removeHandler(handler);
238: }
239:
240: if (tableModel.getRowCount() > 0) {
241: BindingsComponentFactory factory = bindingsModel
242: .getFactory();
243: for (int i = 0; i < tableModel.getRowCount(); i++) {
244: String className = (String) tableModel
245: .getValueAt(i, 0);
246: BindingsHandler handler = factory
247: .createHandler();
248:
249: String handlerName = className
250: .substring(className.indexOf(".") + 1);
251: BindingsHandlerName name = factory
252: .createHandlerName();
253: name.setHandlerName(handlerName);
254: handler.setHandlerName(name);
255:
256: BindingsHandlerClass handlerClass = factory
257: .createHandlerClass();
258: handlerClass.setClassName(className);
259: handler.setHandlerClass(handlerClass);
260:
261: chain.addHandler(handler);
262: }
263: }
264: } finally {
265: bindingsModel.endTransaction();
266: }
267:
268: //save bindingshandler file
269: DataObject dobj = DataObject.find(bindingHandlerFO);
270: if (dobj.isModified()) {
271: SaveCookie saveCookie = dobj
272: .getCookie(SaveCookie.class);
273: saveCookie.save(); //becomes false here
274: }
275:
276: if (tableModel.getRowCount() > 0) {
277: Binding binding = client
278: .getBindingByFileName(bindingsHandlerFile);
279: if (binding == null) {
280: binding = client.newBinding();
281: binding.setFileName(bindingsHandlerFile);
282: client.addBinding(binding);
283: }
284: } else {
285: Binding binding = client
286: .getBindingByFileName(bindingsHandlerFile);
287: if (binding != null) {
288: client.removeBinding(binding);
289: }
290: }
291: //save the jaxws model
292: jaxWsModel.write();
293: boolean l = bindingHandlerFO.isLocked();
294: ErrorManager.getDefault().log(ErrorManager.ERROR,
295: "isLocked: " + l);
296: System.out.println("isLocked: "
297: + bindingHandlerFO.isLocked());
298: } catch (Exception e) {
299: ErrorManager.getDefault().notify(e);
300: }
301: }
302: }
303:
304: //TODO: close all streams properly
305: private static String readResource(InputStream is)
306: throws IOException {
307: // read the config from resource first
308: BufferedReader br = null;
309: InputStreamReader isr = null;
310: StringBuilder sb = new StringBuilder();
311: try {
312:
313: String lineSep = System.getProperty("line.separator");//NOI18N
314: isr = new InputStreamReader(is);
315: br = new BufferedReader(isr);
316:
317: String line = br.readLine();
318: while (line != null) {
319: sb.append(line);
320: sb.append(lineSep);
321: line = br.readLine();
322: }
323: } finally {
324: isr.close();
325: br.close();
326: is.close();
327: }
328: return sb.toString();
329: }
330: }
|