KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > config > ConfigDataLoader


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.sun.share.config;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.HashMap JavaDoc;
25
26 import org.openide.filesystems.FileObject;
27 import org.openide.filesystems.FileUtil;
28 import org.openide.util.Lookup;
29 import org.openide.loaders.DataObjectExistsException;
30 import org.openide.loaders.MultiDataObject;
31 import org.openide.loaders.UniFileLoader;
32 import org.openide.util.NbBundle;
33
34 import org.netbeans.api.project.FileOwnerQuery;
35 import org.netbeans.api.project.Project;
36
37 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
38
39
40 /** Loader for deployment plan files
41  * Permits viewing/editing of them.
42  * @author Pavel Buzek
43  */

44 public class ConfigDataLoader extends UniFileLoader {
45
46     /** Generated serial version UID. */
47 // private static final long serialVersionUID = ;
48

49     private static final String JavaDoc GENERIC_EXTENSION = "dpf"; //NOI18N
50
private static final String JavaDoc PRIMARY = "primary"; //NOI18N
51
private static final String JavaDoc SECONDARY = "secondary"; //NOI18N
52
// private static final String SERVER = "server"; //NOI18N
53

54     private static HashMap JavaDoc primaryByName;
55     private static HashMap JavaDoc secondaryByName;
56     
57     /** Creates loader. */
58     public ConfigDataLoader () {
59         super("org.netbeans.modules.j2ee.sun.share.config.ConfigDataObject"); // NOI18N
60

61         initMaps();
62     }
63
64     /** Initizalized loader, i.e. its extension list. Overrides superclass method. */
65     protected void initialize () {
66         super.initialize();
67     }
68     
69     private void initMaps() {
70         primaryByName = new HashMap JavaDoc();
71         secondaryByName = new HashMap JavaDoc();
72         
73         // Sun Application Server specific files
74
primaryByName.put("sun-web.xml", "WEB-INF/sun-web.xml");
75         primaryByName.put("sun-ejb-jar.xml", "META-INF/sun-ejb-jar.xml");
76         secondaryByName.put("sun-cmp-mappings.xml", "META-INF/sun-cmp-mappings.xml");
77         primaryByName.put("sun-application.xml", "META-INF/sun-application.xml");
78         primaryByName.put("sun-application-client.xml", "META-INF/sun-application-client.xml");
79     }
80     
81     /** Gets default display name. Overrides superclass method. */
82     protected String JavaDoc defaultDisplayName() {
83         return NbBundle.getMessage (ConfigDataLoader.class, "LBL_LoaderName");
84     }
85
86     /** Action available for sun specific deployment descriptor files. See
87      * layer file.
88      */

89     protected String JavaDoc actionsContext() {
90         return "Loaders/xml/sun-dd/Actions/"; // NOI18N
91
}
92     
93     /** Creates multi data object for specified primary file.
94      * Implements superclass abstract method. */

95     protected MultiDataObject createMultiObject (FileObject fo)
96     throws DataObjectExistsException, IOException JavaDoc {
97         if (isPrimaryDescriptor(fo)) {
98             return new ConfigDataObject(fo, this);
99         } else {
100             return new SecondaryConfigDataObject(fo, this);
101         }
102     }
103     
104     private boolean isPrimaryDescriptor(FileObject fo) {
105         String JavaDoc filename = fo.getNameExt();
106         return getPrimaryByName(filename) != null;
107     }
108     
109     protected FileObject findPrimaryFile (FileObject fo) {
110         // never recognize folders.
111
if (fo.isFolder()) {
112             return null;
113         }
114         
115         String JavaDoc ext = fo.getExt();
116         String JavaDoc filename = fo.getNameExt ();
117         FileObject primaryFO = null;
118         String JavaDoc secondaryName = null;
119         if (getPrimaryByName(filename) != null || ext.equals(GENERIC_EXTENSION)) {
120             primaryFO = fo;
121         } else if (getPrimaryBySecondaryName (filename) != null) { // check for secondary file
122
secondaryName = filename;
123         }
124         
125         if (primaryFO == null && secondaryName == null) {
126             return null;
127         }
128         
129         Project owner = FileOwnerQuery.getOwner(fo);
130         if (owner != null) {
131             Lookup l = owner.getLookup();
132             J2eeModuleProvider projectModule = (J2eeModuleProvider) l.lookup(J2eeModuleProvider.class);
133             if (projectModule != null) {
134                 if (primaryFO != null) {
135                     primaryFO = projectModule.findDeploymentConfigurationFile(filename);
136                     if (primaryFO != null) {
137                         File JavaDoc primary = FileUtil.toFile(primaryFO);
138                         if (primary != null && primary.equals(FileUtil.toFile(fo))) {
139                             return fo;
140                         }
141                     }
142                 } else { // look for secondary FO
143
FileObject secondaryFO = projectModule.findDeploymentConfigurationFile(secondaryName);
144                     if(secondaryFO != null) {
145                         File JavaDoc secondary = FileUtil.toFile(secondaryFO);
146                         if (secondary != null && secondary.equals(FileUtil.toFile(fo))) {
147                             return fo;
148                         }
149                     }
150                 }
151             }
152         }
153         return null;
154     }
155     
156     private String JavaDoc getPrimaryByName (String JavaDoc name) {
157         return (String JavaDoc) primaryByName.get(name);
158     }
159     
160     private String JavaDoc getPrimaryBySecondaryName (String JavaDoc name) {
161         return (String JavaDoc) secondaryByName.get(name);
162     }
163 }
164
Popular Tags