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: package org.netbeans.modules.j2ee.sun.share.config;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.util.HashMap;
047: import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
048:
049: import org.openide.filesystems.FileObject;
050: import org.openide.filesystems.FileUtil;
051: import org.openide.util.Lookup;
052: import org.openide.loaders.DataObjectExistsException;
053: import org.openide.loaders.MultiDataObject;
054: import org.openide.loaders.UniFileLoader;
055: import org.openide.util.NbBundle;
056:
057: import org.netbeans.api.project.FileOwnerQuery;
058: import org.netbeans.api.project.Project;
059:
060: import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
061:
062: /** Loader for deployment plan files
063: * Permits viewing/editing of them.
064: * @author Pavel Buzek
065: */
066: public class ConfigDataLoader extends UniFileLoader {
067:
068: /** Generated serial version UID. */
069: // private static final long serialVersionUID = ;
070: private static final String GENERIC_EXTENSION = "dpf"; //NOI18N
071: // private static final String PRIMARY = "primary"; //NOI18N
072: // private static final String SECONDARY = "secondary"; //NOI18N
073: // private static final String SERVER = "server"; //NOI18N
074:
075: private static HashMap primaryByName;
076: private static HashMap secondaryByName;
077:
078: static {
079: primaryByName = new HashMap();
080: secondaryByName = new HashMap();
081:
082: // Sun Application Server specific files
083: primaryByName.put("sun-web.xml", "WEB-INF/sun-web.xml");
084: primaryByName
085: .put("sun-ejb-jar.xml", "META-INF/sun-ejb-jar.xml");
086: secondaryByName.put("sun-cmp-mappings.xml",
087: "META-INF/sun-cmp-mappings.xml");
088: primaryByName.put("sun-application.xml",
089: "META-INF/sun-application.xml");
090: primaryByName.put("sun-application-client.xml",
091: "META-INF/sun-application-client.xml");
092: }
093:
094: /** Creates loader. */
095: public ConfigDataLoader() {
096: super (
097: "org.netbeans.modules.j2ee.sun.share.config.ConfigDataObject"); // NOI18N
098: }
099:
100: /** Initizalized loader, i.e. its extension list. Overrides superclass method. */
101: protected void initialize() {
102: super .initialize();
103: }
104:
105: /** Gets default display name. Overrides superclass method. */
106: protected String defaultDisplayName() {
107: return NbBundle.getMessage(ConfigDataLoader.class,
108: "LBL_LoaderName");
109: }
110:
111: /** Action available for sun specific deployment descriptor files. See
112: * layer file.
113: */
114: protected String actionsContext() {
115: return "Loaders/text/x-sun-dd/Actions/"; // NOI18N
116: }
117:
118: /** Creates multi data object for specified primary file.
119: * Implements superclass abstract method. */
120: protected MultiDataObject createMultiObject(FileObject fo)
121: throws DataObjectExistsException, IOException {
122: MultiDataObject retVal;
123: if (isPrimaryDescriptor(fo)) {
124: retVal = new ConfigDataObject(fo, this );
125: } else {
126: retVal = new SecondaryConfigDataObject(fo, this );
127: }
128: return retVal;
129: }
130:
131: private boolean isPrimaryDescriptor(FileObject fo) {
132: String filename = fo.getNameExt();
133: return getPrimaryByName(filename) != null;
134: }
135:
136: protected FileObject findPrimaryFile(FileObject fo) {
137: // never recognize folders.
138: FileObject retVal = null;
139: if (!fo.isFolder()) {
140:
141: String ext = fo.getExt();
142: String filename = fo.getNameExt();
143: FileObject primaryFO = null;
144: String secondaryName = null;
145: if (getPrimaryByName(filename) != null
146: || ext.equals(GENERIC_EXTENSION)) {
147: primaryFO = fo;
148: } else if (getPrimaryBySecondaryName(filename) != null) { // check for secondary file
149: secondaryName = filename;
150: }
151:
152: if (primaryFO != null || secondaryName != null) {
153:
154: Project owner = FileOwnerQuery.getOwner(fo);
155: if (owner != null) {
156: Lookup l = owner.getLookup();
157: J2eeModuleProvider projectModule = (J2eeModuleProvider) l
158: .lookup(J2eeModuleProvider.class);
159: if (projectModule != null) {
160: J2eeModule mod = projectModule.getJ2eeModule();
161: if (primaryFO != null) {
162: //DDBean Removal
163: primaryFO = FileUtil
164: .toFileObject(mod
165: .getDeploymentConfigurationFile(filename));
166: if (primaryFO != null) {
167: File primary = FileUtil
168: .toFile(primaryFO);
169: if (primary != null
170: && primary.equals(FileUtil
171: .toFile(fo))) {
172: retVal = fo;
173: }
174: }
175: } else { // look for secondary FO
176: // DDBean Removal
177: FileObject secondaryFO = FileUtil
178: .toFileObject(mod
179: .getDeploymentConfigurationFile(secondaryName));
180: if (secondaryFO != null) {
181: File secondary = FileUtil
182: .toFile(secondaryFO);
183: if (secondary != null
184: && secondary.equals(FileUtil
185: .toFile(fo))) {
186: retVal = fo;
187: }
188: }
189: }
190: }
191: }
192: }
193: }
194: return retVal;
195: }
196:
197: private String getPrimaryByName(String name) {
198: return (String) primaryByName.get(name);
199: }
200:
201: private String getPrimaryBySecondaryName(String name) {
202: return (String) secondaryByName.get(name);
203: }
204: }
|