Source Code Cross Referenced for RestSampleUtils.java in  » IDE-Netbeans » web.core » org » netbeans » modules » websvc » rest » samples » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » web.core » org.netbeans.modules.websvc.rest.samples.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:
042:        package org.netbeans.modules.websvc.rest.samples.util;
043:
044:        import java.io.*;
045:        import java.util.Enumeration;
046:        import java.util.Properties;
047:        import java.util.zip.ZipEntry;
048:        import java.util.zip.ZipInputStream;
049:        import org.netbeans.api.project.Project;
050:        import org.netbeans.api.java.classpath.ClassPath;
051:        import org.netbeans.api.java.project.JavaProjectConstants;
052:        import org.netbeans.api.java.project.classpath.ProjectClassPathModifier;
053:        import org.netbeans.api.project.ProjectUtils;
054:        import org.netbeans.api.project.SourceGroup;
055:        import org.netbeans.api.project.libraries.Library;
056:        import org.netbeans.api.project.libraries.LibraryManager;
057:        import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
058:        import org.netbeans.spi.project.support.ant.AntProjectHelper;
059:        import org.netbeans.spi.project.support.ant.EditableProperties;
060:        import org.openide.ErrorManager;
061:        import org.openide.filesystems.FileLock;
062:        import org.openide.filesystems.FileObject;
063:        import org.openide.filesystems.FileUtil;
064:        import org.openide.xml.XMLUtil;
065:        import org.w3c.dom.Document;
066:        import org.w3c.dom.Element;
067:        import org.w3c.dom.Node;
068:        import org.w3c.dom.NodeList;
069:        import org.w3c.dom.Text;
070:        import org.xml.sax.InputSource;
071:
072:        public class RestSampleUtils {
073:
074:            public static final String JERSEY_LIBRARY = "restlib"; //NOI18N
075:            public static String[] xlateFiles = { "build-impl.xml",
076:                    "project.xml", "project.properties",
077:                    "AssemblyInformation.xml" };
078:
079:            public static void unZipFile(InputStream source,
080:                    FileObject rootFolder) throws IOException {
081:                try {
082:                    ZipInputStream str = new ZipInputStream(source);
083:                    ZipEntry entry;
084:                    while ((entry = str.getNextEntry()) != null) {
085:                        if (entry.isDirectory()) {
086:                            FileUtil.createFolder(rootFolder, entry.getName());
087:                            continue;
088:                        }
089:                        FileObject fo = FileUtil.createData(rootFolder, entry
090:                                .getName());
091:                        FileLock lock = fo.lock();
092:                        try {
093:                            OutputStream out = fo.getOutputStream(lock);
094:                            try {
095:                                FileUtil.copy(str, out);
096:                            } finally {
097:                                out.close();
098:                            }
099:                        } finally {
100:                            lock.releaseLock();
101:                        }
102:                    }
103:                } finally {
104:                    source.close();
105:                }
106:            }
107:
108:            public static void unZipFileTranslateProjectName(
109:                    InputStream source, FileObject rootFolder, String name,
110:                    String token) throws IOException {
111:                try {
112:                    ZipInputStream str = new ZipInputStream(source);
113:                    ZipEntry entry;
114:                    while ((entry = str.getNextEntry()) != null) {
115:                        if (entry.isDirectory()) {
116:                            FileUtil.createFolder(rootFolder, entry.getName());
117:                            continue;
118:                        }
119:                        String fname = entry.getName();
120:                        FileObject fo = FileUtil.createData(rootFolder, fname);
121:                        FileLock lock = fo.lock();
122:                        try {
123:                            OutputStream out = fo.getOutputStream(lock);
124:                            try {
125:                                if (needTranslation(fname)) {
126:                                    translateProjectName(str, out, name, token);
127:                                } else {
128:                                    FileUtil.copy(str, out);
129:                                }
130:                            } finally {
131:                                out.close();
132:                            }
133:                        } finally {
134:                            lock.releaseLock();
135:                        }
136:                    }
137:                } finally {
138:                    source.close();
139:                }
140:            }
141:
142:            static boolean needTranslation(String fname) {
143:                for (int i = 0; i < xlateFiles.length; i++) {
144:                    if (fname.endsWith(xlateFiles[i])) {
145:                        return true;
146:                    }
147:                }
148:                return false;
149:            }
150:
151:            static void translateProjectName(InputStream str, OutputStream out,
152:                    String name, String token) throws IOException {
153:                ByteArrayOutputStream bo = new ByteArrayOutputStream();
154:                FileUtil.copy(str, bo);
155:                ByteArrayInputStream bi = new ByteArrayInputStream(bo
156:                        .toString().replaceAll(token, name).getBytes());
157:                FileUtil.copy(bi, out);
158:            }
159:
160:            public static FileObject getStudioUserDir() {
161:                Log.out("StudioUserDir: "
162:                        + FileUtil.toFileObject(
163:                                new File(System.getProperty("netbeans.user")))
164:                                .getPath()); // NOI18N
165:                return FileUtil.toFileObject(new File(System
166:                        .getProperty("netbeans.user"))); // NOI18N
167:            }
168:
169:            public static FileObject getProjectFolder(FileObject parentDir,
170:                    String projectDirName) {
171:                assert parentDir != null : parentDir + "/" + projectDirName
172:                        + "doesn't exist"; // NOI18N
173:                assert projectDirName != null : "project name can't be empty"; // NOI18N
174:                return parentDir.getFileObject(projectDirName);
175:            }
176:
177:            private static FileObject getFolder(String relative) {
178:                return getFolder(getStudioUserDir(), relative);
179:            }
180:
181:            private static FileObject getFolder(FileObject parent,
182:                    String relative) {
183:                FileObject folder = parent.getFileObject(relative);
184:                if (folder != null) {
185:                    return folder;
186:                }
187:                try {
188:                    folder = parent.createFolder(relative);
189:                } catch (IOException ioe) {
190:                    ErrorManager.getDefault().notify(ErrorManager.EXCEPTION,
191:                            ioe);
192:                }
193:                return folder;
194:            }
195:
196:            private static String getPropertiesPath(String name) {
197:                return RestSampleProjectProperties.getDefault()
198:                        .isPrivateProperty(name) ? AntProjectHelper.PRIVATE_PROPERTIES_PATH
199:                        : AntProjectHelper.PROJECT_PROPERTIES_PATH;
200:            }
201:
202:            private static AntProjectHelper getAntProjectHelper(Project project) {
203:                return (AntProjectHelper) project.getLookup().lookup(
204:                        AntProjectHelper.class);
205:            }
206:
207:            public static void setPrivateProperty(FileObject prjLoc,
208:                    String name, String value) {
209:                Properties properties = new Properties();
210:                try {
211:                    properties.setProperty(name, value);
212:
213:                    FileObject propFile = FileUtil.createData(prjLoc,
214:                            AntProjectHelper.PRIVATE_PROPERTIES_PATH);
215:                    FileLock lock = propFile.lock();
216:                    OutputStream os = propFile.getOutputStream(lock);
217:                    try {
218:                        properties.store(os, null);
219:                    } finally {
220:                        os.close();
221:                        lock.releaseLock();
222:                    }
223:                } catch (IOException e) {
224:                    e.printStackTrace();
225:                }
226:            }
227:
228:            public static String getProperty(Project project, String name) {
229:                return getProperties(project, name).getProperty(name);
230:            }
231:
232:            private static EditableProperties getProperties(Project project,
233:                    String name) {
234:                AntProjectHelper helper = getAntProjectHelper(project);
235:                assert helper != null : "Can't get AntProjectHelper for project: "
236:                        + project;
237:                return helper.getProperties(getPropertiesPath(name));
238:            }
239:
240:            /**
241:             * @return SunApp default server instance location
242:             */
243:            public static String getDefaultSunAppLocation() {
244:                String loc = null;
245:                String[] instances = InstanceProperties.getInstanceList();
246:                for (String instanceId : instances) {
247:                    if (instanceId
248:                            .indexOf(RestSampleProjectProperties.SERVER_INSTANCE_SUN_APPSERVER) != -1) {
249:                        int endIdx = instanceId.indexOf(']');
250:                        loc = instanceId.substring(1, endIdx);
251:                        break;
252:                    }
253:                }
254:                return loc;
255:            }
256:
257:            /**
258:             *  Method taken from NB anagram code.
259:             */
260:            public static void setProjectName(FileObject prjLoc,
261:                    String projTypeName, String name) {
262:                try {
263:                    // update project.xml
264:                    File projXml = FileUtil.toFile(prjLoc
265:                            .getFileObject(AntProjectHelper.PROJECT_XML_PATH));
266:                    Document doc = XMLUtil.parse(new InputSource(projXml
267:                            .toURI().toString()), false, true, null, null);
268:                    NodeList nlist = doc.getElementsByTagNameNS(projTypeName,
269:                            "name"); //NOI18N
270:                    if (nlist != null) {
271:                        for (int i = 0; i < nlist.getLength(); i++) {
272:                            Node n = nlist.item(i);
273:                            if (n.getNodeType() != Node.ELEMENT_NODE) {
274:                                continue;
275:                            }
276:                            Element e = (Element) n;
277:
278:                            replaceText(e, name);
279:                        }
280:                        saveXml(doc, prjLoc, AntProjectHelper.PROJECT_XML_PATH);
281:                    }
282:                } catch (Exception e) {
283:                    ErrorManager.getDefault().notify(e);
284:                }
285:            }
286:
287:            /**
288:             * Method taken from NB anagram game.
289:             * Extract nested text from an element.
290:             * Currently does not handle coalescing text nodes, CDATA sections, etc.
291:             * @param parent a parent element
292:             */
293:            private static void replaceText(Element parent, String name) {
294:                NodeList l = parent.getChildNodes();
295:                for (int i = 0; i < l.getLength(); i++) {
296:                    if (l.item(i).getNodeType() == Node.TEXT_NODE) {
297:                        Text text = (Text) l.item(i);
298:                        text.setNodeValue(name);
299:                        return;
300:                    }
301:                }
302:            }
303:
304:            /**
305:             * Method taken from NB anagram game.
306:             * Save an XML config file to a named path.
307:             * If the file does not yet exist, it is created.
308:             */
309:            private static void saveXml(Document doc, FileObject dir,
310:                    String path) throws IOException {
311:                FileObject xml = FileUtil.createData(dir, path);
312:                FileLock lock = xml.lock();
313:                try {
314:                    OutputStream os = xml.getOutputStream(lock);
315:                    try {
316:                        XMLUtil.write(doc, os, "UTF-8"); // NOI18N
317:                    } finally {
318:                        os.close();
319:                    }
320:                } finally {
321:                    lock.releaseLock();
322:                }
323:            }
324:
325:            public static void insertParameters(FileObject dir,
326:                    String bpelProjDir) {
327:                Enumeration files = dir.getData(true);
328:
329:                while (files.hasMoreElements()) {
330:                    FileObject fileObject = (FileObject) files.nextElement();
331:
332:                    if (fileObject.isFolder()) {
333:                        continue;
334:                    }
335:                    if (!((fileObject.getExt().toLowerCase().equals("xml") || fileObject
336:                            .getExt().toLowerCase().equals("properties")))) {
337:                        // NOI18N
338:                        continue;
339:                    }
340:                    String line;
341:                    StringBuffer buffer = new StringBuffer();
342:
343:                    try {
344:                        InputStream inputStream = fileObject.getInputStream();
345:                        BufferedReader reader = new BufferedReader(
346:                                new InputStreamReader(inputStream));
347:
348:                        while ((line = reader.readLine()) != null) {
349:                            line = line.replace("__BPELPROJECTNAME__",
350:                                    bpelProjDir); // NOI18N
351:                            buffer.append(line);
352:                            buffer.append("\n"); // NOI18N
353:                        }
354:
355:                        File file = FileUtil.toFile(fileObject);
356:                        OutputStream outputStream = new FileOutputStream(file);
357:                        PrintWriter writer = new PrintWriter(outputStream);
358:                        writer.write(buffer.toString());
359:                        writer.flush();
360:                        outputStream.close();
361:                    } catch (IOException ex) {
362:                        ErrorManager.getDefault().notify(
363:                                ErrorManager.INFORMATIONAL, ex);
364:                    }
365:                }
366:            }
367:
368:            public static void addJerseyLibrary(Project project) {
369:                Library jerseyLibrary = LibraryManager.getDefault().getLibrary(
370:                        JERSEY_LIBRARY);
371:
372:                if (jerseyLibrary == null) {
373:                    System.out.println("no jersey library found");
374:                    return;
375:                }
376:
377:                SourceGroup[] sgs = ProjectUtils
378:                        .getSources(project)
379:                        .getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
380:                FileObject sourceRoot = sgs[0].getRootFolder();
381:                String[] classPathTypes = new String[] { ClassPath.COMPILE,
382:                        ClassPath.EXECUTE };
383:
384:                for (String type : classPathTypes) {
385:                    try {
386:                        ProjectClassPathModifier.addLibraries(
387:                                new Library[] { jerseyLibrary }, sourceRoot,
388:                                type);
389:                    } catch (Exception ex) {
390:                        ex.printStackTrace();
391:                    }
392:                }
393:            }
394:        }
w___w_w.j__a___v__a__2s___.___c___o__m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.