001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.store;
020:
021: import java.io.Externalizable;
022: import java.io.File;
023: import java.io.IOException;
024: import java.net.MalformedURLException;
025: import java.util.Arrays;
026: import java.util.Collection;
027: import java.util.LinkedList;
028: import java.util.ListIterator;
029:
030: /**
031: * This model contains the list of beans imported by the user for the
032: * application. It also includes all classpaths where these beans are located.
033: *
034: * @author Jeff Tassin
035: */
036: public class ProjectLevelImportedBeansModel implements Externalizable {
037: static final long serialVersionUID = 4699864132660581997L;
038:
039: public static final int VERSION = 2;
040:
041: public static final String COMPONENT_ID = "project.level.imported.beans.model";
042:
043: private String m_env_var;
044:
045: private transient File m_root_dir;
046:
047: /**
048: * A list of ImportedBeanInfo objects that describe each imported java bean
049: */
050: private LinkedList m_imported_beans = new LinkedList();
051:
052: /**
053: * A list of URLs for path and JAR files where the java beans are located.
054: */
055: private LinkedList m_relative_class_paths = new LinkedList();
056:
057: /**
058: * ctor
059: */
060: public ProjectLevelImportedBeansModel() {
061: }
062:
063: public ProjectLevelImportedBeansModel(String envVar) {
064: this .m_env_var = envVar;
065: this .m_root_dir = getRootDir(m_env_var);
066: }
067:
068: /**
069: * @param envVar
070: */
071: private File getRootDir(String envVar) {
072: if (envVar != null) {
073: String directory = System.getenv(envVar);
074: if ((directory != null) && (!"".equals(directory))) {
075: try {
076: File tempFile = new File(directory);
077: if (tempFile.exists() && tempFile.isDirectory()) {
078: return tempFile;
079: }
080: } catch (Exception e) {
081: }
082: }
083: }
084: return null;
085: }
086:
087: /**
088: * Adds an imported bean definition to the model
089: */
090: public void addImportedBean(ImportedBeanInfo bi) {
091: if (m_imported_beans == null) {
092: m_imported_beans = new LinkedList();
093: }
094: if (bi != null) {
095: m_imported_beans.add(bi);
096: }
097: }
098:
099: /**
100: * Adds a relative classpath to the list of classpaths where the java beans
101: * can be found
102: */
103: public void addRelativeClasspath(String relativeClasspath) {
104: if (m_relative_class_paths == null) {
105: m_relative_class_paths = new LinkedList();
106: }
107: if ((relativeClasspath != null)
108: && (!"".equals(relativeClasspath))) {
109: m_relative_class_paths.add(relativeClasspath);
110: }
111: }
112:
113: /**
114: * @return a collection of ImportedBeanInfo objects that define the list of
115: * imported Java beans defined by the user.
116: */
117: public Collection getImportedBeans() {
118: return m_imported_beans;
119: }
120:
121: /**
122: * @return the collection of relative classpaths (String objects) that
123: * define the classpath where the imported Java beans are found.
124: */
125: public Collection getRelativeClasspaths() {
126: return m_relative_class_paths;
127: }
128:
129: /**
130: * @return the collection of urls (URL objects) that define the classpath
131: * where the imported Java beans are found.
132: */
133: public Collection getUrls() {
134: LinkedList urls = new LinkedList();
135: for (ListIterator i = m_relative_class_paths.listIterator(); i
136: .hasNext();) {
137: String relClasspath = (String) i.next();
138: File file = new File(this .m_root_dir, relClasspath);
139: try {
140: urls.addLast(file.toURL());
141: } catch (MalformedURLException e) {
142: e.printStackTrace();
143: }
144: }
145: return urls;
146: }
147:
148: public String getEnvVar() {
149: return this .m_env_var;
150: }
151:
152: public void setEnvVar(String envVar) {
153: this .m_env_var = envVar;
154: this .m_root_dir = getRootDir(this .m_env_var);
155: }
156:
157: public boolean equals(Object obj) {
158: if (this == obj)
159: return true;
160:
161: if (null == obj)
162: return false;
163:
164: if (!(obj instanceof ProjectLevelImportedBeansModel))
165: return false;
166:
167: ProjectLevelImportedBeansModel mObj = (ProjectLevelImportedBeansModel) obj;
168:
169: if ((null == m_env_var && null != mObj.getEnvVar())
170: || (null != m_env_var && null == mObj.getEnvVar())
171: || (null != m_env_var && null != mObj.getEnvVar() && !mObj
172: .getEnvVar().equals(m_env_var))) {
173: return false;
174: } else {
175: if ((null == m_relative_class_paths && null != mObj
176: .getRelativeClasspaths())
177: || (null != m_relative_class_paths && null == mObj
178: .getRelativeClasspaths())
179: || (null != m_relative_class_paths
180: && null != mObj.getRelativeClasspaths() && mObj
181: .getRelativeClasspaths().size() != m_relative_class_paths
182: .size())) {
183: return false;
184: } else {
185: Object[] a1 = m_relative_class_paths.toArray();
186: Arrays.sort(a1);
187:
188: Object[] a2 = mObj.getRelativeClasspaths().toArray();
189: Arrays.sort(a2);
190:
191: for (int i = 0; i < a1.length; i++) {
192: if (!a1[i].equals(a2[i]))
193: return false;
194: }
195:
196: if ((null == m_imported_beans && null != mObj
197: .getImportedBeans())
198: || (null != m_imported_beans && null == mObj
199: .getImportedBeans())
200: || (null != m_imported_beans
201: && null != mObj.getImportedBeans() && mObj
202: .getImportedBeans().size() != m_imported_beans
203: .size())) {
204: return false;
205: } else {
206: Object[] beans1 = m_imported_beans.toArray();
207: Arrays.sort(beans1);
208:
209: Object[] beans2 = mObj.getImportedBeans().toArray();
210: Arrays.sort(beans2);
211:
212: for (int i = 0; i < beans1.length; i++) {
213: if (!beans1[i].equals(beans2[i]))
214: return false;
215: }
216: }
217: }
218: }
219:
220: return true;
221: }
222:
223: /**
224: * Externalizable Implementation
225: */
226: public void readExternal(java.io.ObjectInput in)
227: throws ClassNotFoundException, IOException {
228: int version = in.readInt();
229: m_imported_beans = (LinkedList) in.readObject();
230: m_relative_class_paths = (LinkedList) in.readObject();
231: if (version >= 2) {
232: m_env_var = (String) in.readObject();
233: m_root_dir = getRootDir(m_env_var);
234: }
235: }
236:
237: /**
238: * Externalizable Implementation
239: */
240: public void writeExternal(java.io.ObjectOutput out)
241: throws IOException {
242: out.writeInt(VERSION);
243: out.writeObject(m_imported_beans);
244: out.writeObject(m_relative_class_paths);
245: out.writeObject(m_env_var);
246: }
247: }
|