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: * EjbDataSourceManager.java
043: *
044: * Created on May 4, 2004, 1:13 PM
045: */
046:
047: package org.netbeans.modules.visualweb.ejb;
048:
049: import java.beans.PropertyChangeEvent;
050: import java.beans.PropertyChangeListener;
051: import java.io.BufferedWriter;
052: import java.io.File;
053: import java.io.FileOutputStream;
054: import java.io.IOException;
055: import java.io.OutputStreamWriter;
056: import java.util.Arrays;
057: import java.util.LinkedHashSet;
058: import java.util.List;
059: import java.util.Set;
060: import java.util.logging.Level;
061:
062: import org.netbeans.api.project.Project;
063: import org.netbeans.api.project.ui.OpenProjects;
064: import org.netbeans.modules.visualweb.ejb.datamodel.EjbDataModel;
065: import org.netbeans.modules.visualweb.ejb.load.EjbDataSourceXmlCreator;
066: import org.netbeans.modules.visualweb.ejb.load.EjbDataSourceXmlParser;
067: import org.netbeans.modules.visualweb.ejb.load.EjbLoadException;
068: import org.netbeans.modules.visualweb.ejb.nodes.EjbLibReferenceHelper;
069: import org.netbeans.modules.visualweb.ejb.util.Util;
070: import org.openide.ErrorManager;
071: import org.openide.filesystems.FileUtil;
072:
073: /**
074: * To load and save the data model to xml file
075: *
076: * @author cao
077: */
078: public class EjbDataSourceManager {
079: public static final String EJB_DATA_SUB_DIR = "ejb-sources"; // NOI18N
080:
081: // Singlton
082: private static EjbDataSourceManager mgr = new EjbDataSourceManager();
083:
084: public static EjbDataSourceManager getInstance() {
085: return mgr;
086: }
087:
088: private EjbDataSourceManager() {
089: }
090:
091: /**
092: * Load the Ejb modules from the saved xml files
093: *
094: * @throws IOException
095: */
096: public void load() throws EjbLoadException, IOException {
097: // Load all the information from the xml file if there is one
098:
099: String ejbDataSrcFileName = getUserDirDataSrcFileName();
100: File file = new File(ejbDataSrcFileName);
101: if (file.exists()) {
102: EjbDataSourceXmlParser parser = new EjbDataSourceXmlParser(
103: ejbDataSrcFileName);
104: EjbDataModel.getInstance().addEjbGroups(parser.parse());
105: }
106:
107: // Since everything just loaded, reset the modified flag in the data model
108: EjbDataModel.getInstance().resetModifiedFlag();
109:
110: // Listen on the project open event so that we can check whether any ejb related
111: // jar files needed to be updated
112: OpenProjects.getDefault().addPropertyChangeListener(
113: new PropertyChangeListener() {
114: public void propertyChange(PropertyChangeEvent evt) {
115: if (OpenProjects.PROPERTY_OPEN_PROJECTS
116: .equals(evt.getPropertyName())) {
117: List<Project> oldOpenProjectsList = Arrays
118: .asList((Project[]) evt
119: .getOldValue());
120: List<Project> newOpenProjectsList = Arrays
121: .asList((Project[]) evt
122: .getNewValue());
123:
124: Set<Project> openedProjectsSet = new LinkedHashSet<Project>(
125: newOpenProjectsList);
126: openedProjectsSet
127: .removeAll(oldOpenProjectsList);
128: Project[] openedProjectsArray = openedProjectsSet
129: .toArray(new Project[openedProjectsSet
130: .size()]);
131: EjbLibReferenceHelper
132: .syncArchiveRefs(openedProjectsArray);
133: }
134: }
135: });
136: }
137:
138: /**
139: * Save the data model to the xml files
140: */
141: public void save() {
142: // Do a save only if there is a modification in the data model
143: if (!EjbDataModel.getInstance().isModified())
144: return;
145:
146: String ejbDataSrcFileName;
147: try {
148: ejbDataSrcFileName = getUserDirDataSrcFileName();
149: } catch (IOException e) {
150: Util.getLogger().log(Level.SEVERE,
151: "Unable to save EJB datasource state", e);
152: return;
153: }
154:
155: try {
156: File file = new File(ejbDataSrcFileName);
157:
158: if (!file.exists())
159: file.createNewFile();
160:
161: OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
162: new FileOutputStream(file), "UTF-8"); // No I18N
163: BufferedWriter bufferWriter = new BufferedWriter(
164: outputStreamWriter);
165:
166: EjbDataSourceXmlCreator creator = new EjbDataSourceXmlCreator(
167: EjbDataModel.getInstance(), bufferWriter);
168: creator.toXml();
169: bufferWriter.flush();
170: bufferWriter.close();
171: } catch (Exception ex) {
172: ErrorManager
173: .getDefault()
174: .getInstance(
175: "org.netbeans.modules.visualweb.ejb.EjbDataSourceManager")
176: .log(
177: ErrorManager.ERROR,
178: "Failed to save ejb datasource to file: "
179: + ejbDataSrcFileName);
180: ex.printStackTrace();
181: }
182: }
183:
184: private static String getUserDirDataSrcFileName()
185: throws IOException {
186: File ejbDataSourceDir = new File(Util.getEjbStateDir(),
187: "ejb-datasource");
188: if (!ejbDataSourceDir.isDirectory()) {
189: // XXX This is broken since it does not update an existing ejbsources.xml file
190: // with the updated location
191:
192: // Try to migrate EJB DataSources from old to new location
193: String userDir = System.getProperty("netbeans.user"); // NOI18N
194: if (userDir != null) {
195: File legacyDir = new File(userDir, "ejb-datasource"); // NOI18N
196: if (legacyDir.exists()) {
197: Util.copyFileRecursive(legacyDir, ejbDataSourceDir);
198:
199: // Try to remove the old directory
200: try {
201: FileUtil.toFileObject(legacyDir).delete();
202: } catch (Exception e) {
203: Util
204: .getLogger()
205: .log(
206: Level.WARNING,
207: "Unable to remove legacy ejb-datasource dir",
208: e);
209: }
210: }
211: }
212: }
213:
214: return new File(ejbDataSourceDir, "ejbsources.xml")
215: .getAbsolutePath();
216: }
217: }
|