001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id$
023: */
024:
025: package com.bostechcorp.cbesb.ui.ide.classpath;
026:
027: import java.io.File;
028: import java.io.FileFilter;
029:
030: import org.eclipse.core.runtime.CoreException;
031: import org.eclipse.core.runtime.IPath;
032: import org.eclipse.core.runtime.IProgressMonitor;
033: import org.eclipse.core.runtime.NullProgressMonitor;
034: import org.eclipse.core.runtime.Path;
035: import org.eclipse.jdt.core.ClasspathVariableInitializer;
036: import org.eclipse.jdt.core.JavaCore;
037: import org.eclipse.ui.plugin.AbstractUIPlugin;
038:
039: import com.bostechcorp.cbesb.common.i18n.I18N;
040: import com.bostechcorp.cbesb.common.i18n.Messages;
041: import com.bostechcorp.cbesb.common.util.EsbPathHelper;
042: import com.bostechcorp.cbesb.ui.ide.Activator;
043:
044: public abstract class AbstractClasspathVariableInitializer extends
045: ClasspathVariableInitializer {
046: private IProgressMonitor fMonitor;
047:
048: /**Constructor for the AbstractClasspathVariableInitializer object */
049: public AbstractClasspathVariableInitializer() {
050: }
051:
052: /**
053: * Description of the Method
054: *
055: * @param variable Description of the Parameter
056: */
057: public void initialize(String variable) {
058: IPath newPath = searchJar(Activator.getDefault());
059: if (newPath != null) {
060: try {
061: this .setJ2EEVariable(variable, newPath);
062: } catch (CoreException ce) { //"Cannot initialize variable"
063: Activator.getDefault().logError(
064: I18N.getString(Messages.IDE_ERR_CANNOT_INIT),
065: ce);//$NON-NLS-1$
066: }
067: }
068: }
069:
070: /**
071: * Gets the jarName attribute of the AbstractClasspathVariableInitializer object
072: *
073: * @return The jarName value
074: */
075: protected abstract String getJarName();
076:
077: /**
078: * Gets the libFolder attribute of the AbstractClasspathVariableInitializer object
079: *
080: * @return The libFolder value
081: */
082: protected abstract String getLibFolder();
083:
084: /**
085: * Gets the monitor attribute of the AbstractClasspathVariableInitializer object
086: *
087: * @return The monitor value
088: */
089: protected IProgressMonitor getMonitor() {
090: if (fMonitor == null) {
091: return new NullProgressMonitor();
092: }
093: return fMonitor;
094: }
095:
096: /**
097: * Description of the Method
098: *
099: * @param plugin Description of the Parameter
100: * @return Description of the Return Value
101: */
102: protected IPath searchJar(AbstractUIPlugin plugin) {
103: IPath result = null;
104: String libPath = "";
105:
106: String home = EsbPathHelper.getCbesbHomeDir();
107: if (home != null) {
108: libPath = home + "/" + ClassPathConstants.LIB_FOLDER + "/"
109: + getLibFolder();
110:
111: } else {
112: String baseDir = Activator.getDefault().getBaseDir();
113: libPath = baseDir + "/lib/" + getLibFolder();
114:
115: }
116:
117: File libDir = new File(libPath);//$NON-NLS-1$ //$NON-NLS-2$
118:
119: // Search for the jar
120: final String searchedJar = this .getJarName();
121: File[] jars = libDir.listFiles(new FileFilter() {
122: public boolean accept(File file) {
123: return (file.getName().equals(searchedJar));//$NON-NLS-1$
124: }
125: });
126:
127: if (jars.length == 1) {
128: File jarFile = jars[0];
129: result = new Path(jarFile.toString());
130: }
131: return result;
132: }
133:
134: /**
135: * Description of the Method
136: *
137: * @param variable Description of the Parameter
138: * @param newPath Description of the Parameter
139: * @return Description of the Return Value
140: * @exception CoreException Description of the Exception
141: */
142: private boolean changedJ2EEVariable(String variable, IPath newPath)
143: throws CoreException {
144: IPath oldPath = JavaCore.getClasspathVariable(variable);
145: return !newPath.equals(oldPath);
146: }
147:
148: /**
149: * Sets the j2EEVariable attribute of the AbstractClasspathVariableInitializer object
150: *
151: * @param variable The new j2EEVariable value
152: * @param newPath The new j2EEVariable value
153: * @exception CoreException Description of the Exception
154: */
155: private void setJ2EEVariable(String variable, IPath newPath)
156: throws CoreException {
157: if (this.changedJ2EEVariable(variable, newPath)) {
158: JavaCore.setClasspathVariable(variable, newPath,
159: getMonitor());
160: }
161: }
162: }
|