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-2007 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: * Util.java
043: *
044: * Created on June 9, 2004, 2:56 PM
045: */
046:
047: package org.netbeans.modules.visualweb.ejb.util;
048:
049: import java.io.File;
050: import java.io.FileInputStream;
051: import java.io.FileOutputStream;
052: import java.io.IOException;
053: import java.nio.channels.FileChannel;
054: import java.util.logging.Logger;
055:
056: import org.openide.filesystems.FileUtil;
057: import org.openide.filesystems.Repository;
058:
059: /**
060: * This class contains some utility methods
061: *
062: * @author cao
063: */
064: public class Util {
065:
066: /** Logger for visualweb EJB module */
067: private static final Logger logger;
068:
069: /** Directory in userdir where this modules state is kept */
070: private static File ejbStateDir;
071:
072: static {
073: String utilPkg = Util.class.getPackage().getName();
074: int lastIndex = utilPkg.lastIndexOf(".");
075: String codeNameBase = utilPkg.substring(0, lastIndex);
076: logger = Logger.getLogger(codeNameBase);
077: }
078:
079: /**
080: * Get the file name out of the path
081: */
082: public static String getFileName(String path) {
083: return new File(path).getName();
084: }
085:
086: public static String getClassName(String fullPackageClassName) {
087: int i = fullPackageClassName.lastIndexOf(".");
088: String beanClassName = fullPackageClassName.substring(i + 1);
089:
090: return beanClassName;
091: }
092:
093: public static String getPackageName(String fullPackageClassName) {
094: int i = fullPackageClassName.lastIndexOf(".");
095:
096: if (i == -1) // No package
097: i = 0;
098:
099: String packageName = fullPackageClassName.substring(0, i);
100:
101: return packageName;
102: }
103:
104: /*
105: * Utility routine to paper over array type names
106: */
107: public static String getTypeName(Class type) {
108: if (type.isArray()) {
109: try {
110: Class cl = type;
111: int dimensions = 0;
112: while (cl.isArray()) {
113: dimensions++;
114: cl = cl.getComponentType();
115: }
116: StringBuffer sb = new StringBuffer();
117: sb.append(cl.getName());
118: for (int i = 0; i < dimensions; i++) {
119: sb.append("[]");
120: }
121: return sb.toString();
122: } catch (Throwable e) { /* FALLTHRU */
123: }
124: }
125: return type.getName();
126: }
127:
128: public static String capitalize(String name) {
129: if (name == null || name.length() == 0) {
130: return name;
131: }
132:
133: char chars[] = name.toCharArray();
134: chars[0] = Character.toUpperCase(chars[0]);
135: return new String(chars);
136: }
137:
138: public static String decapitalize(String name) {
139: if (name == null || name.length() == 0) {
140: return name;
141: }
142:
143: char chars[] = name.toCharArray();
144: chars[0] = Character.toLowerCase(chars[0]);
145: return new String(chars);
146: }
147:
148: public static boolean isAcronyn(String name) {
149: // The name is consider as acronyn if it starts with two or more upper-case letters in a row
150:
151: if (name == null || name.length() == 0) {
152: return false;
153: }
154:
155: char chars[] = name.toCharArray();
156: if (Character.isUpperCase(chars[0])
157: && Character.isUpperCase(chars[1]))
158: return true;
159: else
160: return false;
161: }
162:
163: public static Logger getLogger() {
164: return logger;
165: }
166:
167: /**
168: * Returns the NetBeans module code name base.
169: *
170: * @return
171: */
172: public static String getCodeNameBase() {
173: // Code name base should be same as the package name of this class minus the last component
174: String pkgName = Util.class.getPackage().getName();
175: return pkgName.substring(0, pkgName.lastIndexOf('.'));
176: }
177:
178: /**
179: * Get the directory where state of this module is kept. This will be in the userdir and also
180: * part of the NetBeans filesystem so that it will make it easier to migrate to future IDE
181: * versions.
182: *
183: * @return
184: * @throws IOException
185: */
186: public static File getEjbStateDir() {
187: if (ejbStateDir == null) {
188: File root = FileUtil.toFile(Repository.getDefault()
189: .getDefaultFileSystem().getRoot());
190: // Follow NetBeans convention of replacing dots with dashes
191: String stateDirName = getCodeNameBase().replace('.', '-');
192: ejbStateDir = new File(root, stateDirName);
193: if (!ejbStateDir.exists() && !ejbStateDir.mkdirs()) {
194: IllegalStateException ex = new IllegalStateException(
195: "Unable to create dir: " + ejbStateDir);
196: throw ex;
197: }
198: }
199: return ejbStateDir;
200: }
201:
202: public static void copyFileRecursive(File source, File dest)
203: throws IOException {
204: File newItem = null;
205: if (dest.isDirectory()) {
206: newItem = new File(dest, source.getName());
207: } else {
208: newItem = dest;
209: }
210:
211: if (source.isDirectory()) {
212: newItem.mkdir();
213: File[] contents = source.listFiles();
214:
215: for (int i = 0; i < contents.length; i++) {
216: copyFileRecursive(contents[i], newItem);
217: }
218: } else {
219: copyFile(source, newItem);
220: }
221: }
222:
223: /**
224: * Copy files using nio
225: *
226: * @param src
227: * source file
228: * @param dst
229: * destination file
230: * @throws IOException
231: */
232: public static void copyFile(File src, File dst) throws IOException {
233: FileChannel srcChannel = new FileInputStream(src).getChannel();
234: FileChannel dstChannel = new FileOutputStream(dst).getChannel();
235: dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
236: srcChannel.close();
237: dstChannel.close();
238: }
239: }
|