diff options
author | Benoit Foucher <benoit@zeroc.com> | 2009-03-24 11:45:18 +0100 |
---|---|---|
committer | Benoit Foucher <benoit@zeroc.com> | 2009-03-24 11:45:18 +0100 |
commit | 06a08ecf28e205277336a97a6173db7ccbed1adc (patch) | |
tree | a369a5044a63f8cdba9e7c0a461e24ae344486b4 /eclipse/Slice2javaPlugin/src/com/zeroc/slice2javaplugin/internal | |
parent | Merge branch 'R3_3_branch' (diff) | |
parent | Bug 3924: slice2py missing from VC60 installer (diff) | |
download | ice-06a08ecf28e205277336a97a6173db7ccbed1adc.tar.bz2 ice-06a08ecf28e205277336a97a6173db7ccbed1adc.tar.xz ice-06a08ecf28e205277336a97a6173db7ccbed1adc.zip |
Merge commit 'origin/R3_3_branch'
Conflicts:
CHANGES
cpp/demo/Freeze/backup/.depend
cpp/demo/Freeze/bench/.depend
cpp/demo/Freeze/casino/.depend
cpp/demo/Freeze/customEvictor/.depend
cpp/demo/Freeze/library/.depend
cpp/demo/Freeze/phonebook/.depend
cpp/demo/Freeze/transform/.depend
cpp/demo/Glacier2/callback/.depend
cpp/demo/Glacier2/chat/.depend
cpp/demo/Ice/async/.depend
cpp/demo/Ice/bidir/.depend
cpp/demo/Ice/callback/.depend
cpp/demo/Ice/converter/.depend
cpp/demo/Ice/hello/.depend
cpp/demo/Ice/invoke/.depend
cpp/demo/Ice/latency/.depend
cpp/demo/Ice/minimal/.depend
cpp/demo/Ice/multicast/.depend
cpp/demo/Ice/nested/.depend
cpp/demo/Ice/nrvo/.depend
cpp/demo/Ice/session/.depend
cpp/demo/Ice/throughput/.depend
cpp/demo/Ice/value/.depend
cpp/demo/IceBox/hello/.depend
cpp/demo/IceGrid/allocate/.depend
cpp/demo/IceGrid/icebox/.depend
cpp/demo/IceGrid/replication/.depend
cpp/demo/IceGrid/sessionActivation/.depend
cpp/demo/IceGrid/simple/.depend
cpp/demo/IceStorm/clock/.depend
cpp/demo/IceStorm/counter/.depend
cpp/demo/IceStorm/replicated/.depend
cpp/demo/IceStorm/replicated2/.depend
cpp/demo/book/freeze_filesystem/.depend
cpp/demo/book/lifecycle/.depend
cpp/demo/book/printer/.depend
cpp/demo/book/simple_filesystem/.depend
cpp/src/Freeze/.depend
cpp/src/FreezeScript/.depend
cpp/src/Ice/.depend
cpp/src/Ice/UdpTransceiver.cpp
cpp/src/Ice/UdpTransceiver.h
cpp/src/IceBox/.depend
cpp/src/IceGrid/.depend
cpp/src/IceGridLib/.depend
cpp/src/IcePatch2/.depend
cpp/src/IceStorm/.depend
cpp/src/slice2freeze/.depend
cpp/test/Freeze/complex/.depend
cpp/test/Freeze/dbmap/.depend
cpp/test/Freeze/evictor/.depend
cpp/test/Freeze/oldevictor/.depend
cpp/test/FreezeScript/dbmap/.depend
cpp/test/FreezeScript/evictor/.depend
cpp/test/Glacier2/attack/.depend
cpp/test/Glacier2/dynamicFiltering/.depend
cpp/test/Glacier2/router/.depend
cpp/test/Glacier2/sessionControl/.depend
cpp/test/Glacier2/ssl/.depend
cpp/test/Glacier2/staticFiltering/.depend
cpp/test/Ice/adapterDeactivation/.depend
cpp/test/Ice/background/.depend
cpp/test/Ice/binding/.depend
cpp/test/Ice/checksum/.depend
cpp/test/Ice/checksum/server/.depend
cpp/test/Ice/custom/.depend
cpp/test/Ice/exceptions/.depend
cpp/test/Ice/facets/.depend
cpp/test/Ice/faultTolerance/.depend
cpp/test/Ice/gc/.depend
cpp/test/Ice/hold/.depend
cpp/test/Ice/inheritance/.depend
cpp/test/Ice/interceptor/.depend
cpp/test/Ice/location/.depend
cpp/test/Ice/objects/.depend
cpp/test/Ice/operations/.depend
cpp/test/Ice/proxy/.depend
cpp/test/Ice/retry/.depend
cpp/test/Ice/servantLocator/.depend
cpp/test/Ice/slicing/exceptions/.depend
cpp/test/Ice/slicing/objects/.depend
cpp/test/Ice/stream/.depend
cpp/test/Ice/stringConverter/.depend
cpp/test/Ice/timeout/.depend
cpp/test/Ice/udp/.depend
cpp/test/IceBox/configuration/.depend
cpp/test/IceGrid/activation/.depend
cpp/test/IceGrid/allocation/.depend
cpp/test/IceGrid/deployer/.depend
cpp/test/IceGrid/distribution/.depend
cpp/test/IceGrid/replicaGroup/.depend
cpp/test/IceGrid/replication/.depend
cpp/test/IceGrid/session/.depend
cpp/test/IceGrid/simple/.depend
cpp/test/IceGrid/update/.depend
cpp/test/IceSSL/configuration/.depend
cpp/test/IceStorm/federation/.depend
cpp/test/IceStorm/federation2/.depend
cpp/test/IceStorm/rep1/.depend
cpp/test/IceStorm/repgrid/.depend
cpp/test/IceStorm/repstress/.depend
cpp/test/IceStorm/single/.depend
cpp/test/IceStorm/stress/.depend
cpp/test/Slice/keyword/.depend
cs/src/Ice/Instance.cs
cs/src/IceSSL/ConnectorI.cs
java/demo/book/simple_filesystem/Filesystem/DirectoryI.java
java/demo/book/simple_filesystem/Filesystem/FileI.java
java/src/IceInternal/TcpConnector.java
java/src/IceSSL/ConnectorI.java
py/modules/IcePy/.depend
rb/src/IceRuby/.depend
Diffstat (limited to 'eclipse/Slice2javaPlugin/src/com/zeroc/slice2javaplugin/internal')
4 files changed, 1412 insertions, 0 deletions
diff --git a/eclipse/Slice2javaPlugin/src/com/zeroc/slice2javaplugin/internal/Configuration.java b/eclipse/Slice2javaPlugin/src/com/zeroc/slice2javaplugin/internal/Configuration.java new file mode 100644 index 00000000000..d5197a594ec --- /dev/null +++ b/eclipse/Slice2javaPlugin/src/com/zeroc/slice2javaplugin/internal/Configuration.java @@ -0,0 +1,738 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved. +// +// This plug-in is provided to you under the terms and conditions +// of the Eclipse Public License Version 1.0 ("EPL"). A copy of +// the EPL is available at http://www.eclipse.org/legal/epl-v10.html. +// +// ********************************************************************** + +package com.zeroc.slice2javaplugin.internal; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.ProjectScope; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.preferences.InstanceScope; +import org.eclipse.jdt.core.IClasspathEntry; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.ui.preferences.ScopedPreferenceStore; + +import com.zeroc.slice2javaplugin.Activator; +import com.zeroc.slice2javaplugin.preferences.PluginPreferencePage; + +public class Configuration +{ + public Configuration(IProject project) + { + _project = project; + + _instanceStore = new ScopedPreferenceStore(new InstanceScope(), Activator.PLUGIN_ID + "." + _project.getName()); + + _store = new ScopedPreferenceStore(new ProjectScope(project), Activator.PLUGIN_ID); + + _store.setDefault(GENERATED_KEY, GENERATED_KEY); + _store.setDefault(DEFINES_KEY, ""); + _store.setDefault(TIE_KEY, false); + _store.setDefault(ICE_KEY, false); + _store.setDefault(STREAM_KEY, false); + _store.setDefault(ICE_INCLUDE_KEY, false); + _store.setDefault(META_KEY, ""); + _store.setDefault(CONSOLE_KEY, false); + _store.setDefault(SLICE_SOURCE_DIRS_KEY, "slice"); + _store.setDefault(INCLUDES_KEY, ""); + boolean androidProject = false; + try + { + androidProject = project.hasNature("com.android.ide.eclipse.adt.AndroidNature"); + } + catch(CoreException e) + { + } + if(androidProject) + { + // TODO: At present android does not work with indirect libraries. + //_store.setDefault(JARS_KEY, "IceAndroid.jar"); + _store.setDefault(JARS_KEY, ""); + } + else + { + _store.setDefault(JARS_KEY, "Ice.jar"); + } + } + + /** + * Turns list of strings into a single ';' delimited string. ';' in the + * string values are escaped with a leading '\'. '\' are turned into '\\'. + * + * @param l + * List of strings. + * @return Semicolon delimited string. + */ + static public String fromList(List<String> l) + { + StringBuffer sb = new StringBuffer(); + for(Iterator<String> p = l.iterator(); p.hasNext();) + { + if(sb.length() > 0) + { + sb.append(";"); + } + sb.append(escape(p.next())); + } + return sb.toString(); + } + + /** + * Turn a semicolon delimited string into a list of strings. Escaped values + * are preserved (characters prefixed with a '\'). + * + * @param s + * Semicolon delimited string. + * @return List of strings. + */ + static public List<String> toList(String s) + { + java.util.List<String> l = new ArrayList<String>(); + int curr = 0; + int end = s.length(); + boolean escape = false; + StringBuffer sb = new StringBuffer(); + for(curr = 0; curr < end; ++curr) + { + char ch = s.charAt(curr); + if(escape) + { + sb.append(ch); + escape = false; + } + else + { + if(ch == ';') + { + String tok = sb.toString().trim(); + sb.setLength(0); + if(tok.length() > 0) + { + l.add(tok); + } + } + else if(ch == '\\') + { + escape = true; + } + else + { + sb.append(ch); + } + } + } + String tok = sb.toString().trim(); + if(tok.length() > 0) + { + l.add(tok); + } + return l; + } + + public boolean write() + throws CoreException, IOException + { + boolean rc = false; + if(_store.needsSaving()) + { + _store.save(); + rc = true; + } + if(_instanceStore.needsSaving()) + { + _instanceStore.save(); + rc = true; + } + + return rc; + } + + public void initialize() + throws CoreException + { + // Create the slice source directories, if necessary. + for(Iterator<String> p = getSliceSourceDirs().iterator(); p.hasNext();) + { + IFolder slice = _project.getFolder(p.next()); + if(!slice.exists()) + { + slice.create(false, true, null); + } + } + + // Create the generated directory, if necessary. + IFolder generated = _project.getFolder(getGeneratedDir()); + if(!generated.exists()) + { + generated.create(false, true, null); + } + + fixGeneratedCP(null, getGeneratedDir()); + + IJavaProject javaProject = JavaCore.create(_project); + + IClasspathEntry cpEntry = IceClasspathContainerIntializer.getContainerEntry(); + IClasspathEntry[] entries = javaProject.getRawClasspath(); + + boolean found = false; + for(int i = 0; i < entries.length; ++i) + { + if(entries[i].equals(cpEntry)) + { + found = true; + break; + } + } + + if(!found) + { + IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1]; + System.arraycopy(entries, 0, newEntries, 0, entries.length); + newEntries[entries.length] = cpEntry; + + try + { + javaProject.setRawClasspath(newEntries, null); + } + catch(JavaModelException e) + { + throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.toString(), null)); + } + } + } + + public void deinstall() + throws CoreException + { + IJavaProject javaProject = JavaCore.create(_project); + + IClasspathEntry cpEntry = IceClasspathContainerIntializer.getContainerEntry(); + IClasspathEntry[] entries = javaProject.getRawClasspath(); + + for(int i = 0; i < entries.length; ++i) + { + if(entries[i].equals(cpEntry)) + { + IClasspathEntry[] newEntries = new IClasspathEntry[entries.length - 1]; + System.arraycopy(entries, 0, newEntries, 0, i); + System.arraycopy(entries, i + 1, newEntries, i, entries.length - i - 1); + + try + { + javaProject.setRawClasspath(newEntries, null); + } + catch(JavaModelException e) + { + throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.toString(), null)); + } + break; + } + } + } + + public List<String> getSliceSourceDirs() + { + return toList(_store.getString(SLICE_SOURCE_DIRS_KEY)); + } + + public void setSliceSourceDirs(List<String> sliceSourceDirs) + { + setValue(SLICE_SOURCE_DIRS_KEY, fromList(sliceSourceDirs)); + } + + public String getGeneratedDir() + { + return _store.getString(GENERATED_KEY); + } + + public void fixGeneratedCP(String oldG, String newG) + throws CoreException + { + IJavaProject javaProject = JavaCore.create(_project); + + IFolder newGenerated = _project.getFolder(newG); + + IClasspathEntry[] entries = javaProject.getRawClasspath(); + IClasspathEntry newEntry = JavaCore.newSourceEntry(newGenerated.getFullPath()); + + if(oldG != null) + { + IFolder oldGenerated = _project.getFolder(oldG); + IClasspathEntry oldEntry = JavaCore.newSourceEntry(oldGenerated.getFullPath()); + for(int i = 0; i < entries.length; ++i) + { + if(entries[i].equals(oldEntry)) + { + entries[i] = newEntry; + javaProject.setRawClasspath(entries, null); + return; + } + } + } + + IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1]; + System.arraycopy(entries, 0, newEntries, 1, entries.length); + newEntries[0] = newEntry; + + try + { + javaProject.setRawClasspath(newEntries, null); + } + catch(JavaModelException e) + { + // This can occur if a duplicate CLASSPATH entry is made. + // + // throw new CoreException(new Status(IStatus.ERROR, + // Activator.PLUGIN_ID, e.toString(), null)); + } + } + + public void setGeneratedDir(String generated) + throws CoreException + { + String oldGenerated = getGeneratedDir(); + if(setValue(GENERATED_KEY, generated)) + { + fixGeneratedCP(oldGenerated, generated); + } + } + + public List<String> getCommandLine() + { + List<String> cmds = new ArrayList<String>(); + for(Iterator<String> p = getIncludes().iterator(); p.hasNext();) + { + cmds.add("-I" + p.next()); + } + for(Iterator<String> p = getDefines().iterator(); p.hasNext();) + { + cmds.add("-D" + p.next()); + } + for(Iterator<String> p = getMeta().iterator(); p.hasNext();) + { + cmds.add("--meta"); + cmds.add(p.next()); + } + if(getStream()) + { + cmds.add("--stream"); + } + if(getTie()) + { + cmds.add("--tie"); + } + if(getIce()) + { + cmds.add("--ice"); + } + + return cmds; + } + + public boolean getIceInclude() + { + return _store.getBoolean(ICE_INCLUDE_KEY); + } + + public void setIceInclude(boolean selection) + { + _store.setValue(ICE_INCLUDE_KEY, selection); + } + + public List<String> getIncludes() + { + List<String> s = toList(_store.getString(INCLUDES_KEY)); + if(getIceInclude()) + { + String iceHome = getIceHome(); + String os = System.getProperty("os.name"); + String path = null; + if(os.equals("Linux") && iceHome.equals("/usr")) + { + String version = getIceVersion(); + if(version != null) + { + File f = new File("/usr/share/Ice-" + version + "/slice"); + if(f.exists()) + { + path = f.toString(); + } + } + } + + if(path == null) + { + path = new File(iceHome + File.separator + "slice").toString(); + } + + s.add(path); + } + return s; + } + + // The bare include list. + public List<String> getBareIncludes() + { + return toList(_store.getString(INCLUDES_KEY)); + } + + public void setIncludes(List<String> includes) + { + setValue(INCLUDES_KEY, fromList(includes)); + } + + public List<String> getJars() + { + return toList(_store.getString(JARS_KEY)); + } + + public void setJars(List<String> jars) throws CoreException + { + if(setValue(JARS_KEY, fromList(jars))) + { + IceClasspathContainerIntializer.reinitialize(_project, this); + } + } + + public List<String> getDefines() + { + return toList(_store.getString(DEFINES_KEY)); + } + + public void setDefines(List<String> defines) + { + setValue(DEFINES_KEY, fromList(defines)); + } + + public boolean getStream() + { + return _store.getBoolean(STREAM_KEY); + } + + public void setStream(boolean stream) + { + _store.setValue(STREAM_KEY, stream); + } + + public boolean getTie() + { + return _store.getBoolean(TIE_KEY); + } + + public void setTie(boolean tie) + { + _store.setValue(TIE_KEY, tie); + } + + public boolean getIce() + { + return _store.getBoolean(ICE_KEY); + } + + public void setIce(boolean ice) + { + _store.setValue(ICE_KEY, ice); + } + + public boolean getConsole() + { + return _store.getBoolean(CONSOLE_KEY); + } + + public void setConsole(boolean console) + { + _store.setValue(CONSOLE_KEY, console); + } + + public List<String> getMeta() + { + return toList(_store.getString(META_KEY)); + } + + public void setMeta(List<String> meta) + { + setValue(META_KEY, fromList(meta)); + } + + public void setupSharedLibraryPath(Map<String, String> env) + { + String iceHome = getIceHome(); + + String libPath; + boolean srcdist = false; + if(new File(iceHome + File.separator + "cpp" + File.separator + "bin").exists()) + { + // iceHome points at a source distribution. + libPath = new File(iceHome + File.separator + "cpp" + File.separator + "lib").toString(); + srcdist = true; + } + else + { + libPath = new File(iceHome + File.separator + "lib").toString(); + } + + String ldLibPathEnv = null; + String ldLib64PathEnv = null; + String lib64Path = null; + + String os = System.getProperty("os.name"); + if(os.equals("Mac OS X")) + { + ldLibPathEnv = "DYLD_LIBRARY_PATH"; + } + else if(os.equals("AIX")) + { + ldLibPathEnv = "LIBPATH"; + } + else if(os.equals("HP-UX")) + { + ldLibPathEnv = "SHLIB_PATH"; + ldLib64PathEnv = "LD_LIBRARY_PATH"; + if(srcdist) + { + lib64Path = libPath; + } + else + { + lib64Path = new File(iceHome + File.separator + "lib" + File.separator + "pa20_64").toString(); + } + } + else if(os.startsWith("Windows")) + { + // + // No need to change the PATH environment variable on Windows, the + // DLLs should be found + // in the translator local directory. + // + // ldLibPathEnv = "PATH"; + } + else if(os.equals("SunOS")) + { + ldLibPathEnv = "LD_LIBRARY_PATH"; + ldLib64PathEnv = "LD_LIBRARY_PATH_64"; + String arch = System.getProperty("os.arch"); + if(srcdist) + { + lib64Path = libPath; + } + else if(arch.equals("x86")) + { + lib64Path = new File(iceHome + File.separator + "lib" + File.separator + "amd64").toString(); + } + else + // Sparc + { + lib64Path = new File(iceHome + File.separator + "lib" + File.separator + "sparcv9").toString(); + } + } + else + { + ldLibPathEnv = "LD_LIBRARY_PATH"; + ldLib64PathEnv = "LD_LIBRARY_PATH"; + if(srcdist) + { + lib64Path = libPath; + } + else + { + lib64Path = new File(iceHome + File.separator + "lib64").toString(); + } + } + + if(ldLibPathEnv != null) + { + if(ldLibPathEnv.equals(ldLib64PathEnv)) + { + libPath = libPath + File.pathSeparator + lib64Path; + } + + String envLibPath = env.get(ldLibPathEnv); + if(envLibPath != null) + { + libPath = libPath + File.pathSeparator + envLibPath; + } + + env.put(ldLibPathEnv, libPath); + } + + if(ldLib64PathEnv != null && !ldLib64PathEnv.equals(ldLibPathEnv)) + { + String envLib64Path = env.get(ldLib64PathEnv); + if(envLib64Path != null) + { + lib64Path = lib64Path + File.pathSeparator + envLib64Path; + } + env.put(ldLib64PathEnv, lib64Path); + } + } + + public String getTranslator() + { + return getTranslatorForHome(getIceHome()); + } + + static public boolean verifyIceHome(String dir) + { + return getTranslatorForHome(dir) != null; + } + + public String getJarDir() + { + String iceHome = getIceHome(); + String os = System.getProperty("os.name"); + if(os.equals("Linux") && iceHome.equals("/usr")) + { + File f = new File(iceHome + File.separator + "share" + File.separator + "java"); + if(f.exists()) + { + return f.toString(); + } + } + + File f = new File(iceHome + File.separator + "lib"); + if(!f.exists()) + { + File f2 = new File(iceHome + File.separator + "java" + File.separator + "lib"); + if(f2.exists()) + { + return f2.toString(); + } + } + // Add the platform default even if it cannot be found. + return f.toString(); + } + + private String getIceHome() + { + return Activator.getDefault().getPreferenceStore().getString(PluginPreferencePage.SDK_PATH); + } + + // For some reason ScopedPreferenceStore.setValue(String, String) + // doesn't check to see whether the stored value is the same as + // the new value. + private boolean setValue(String key, String value) + { + return setValue(_store, key, value); + } + + private boolean setValue(ScopedPreferenceStore store, String key, String value) + { + if(!store.getString(key).equals(value)) + { + store.setValue(key, value); + return true; + } + return false; + } + + static private String escape(String s) + { + int curr = 0; + int end = s.length(); + StringBuffer sb = new StringBuffer(); + for(curr = 0; curr < end; ++curr) + { + char ch = s.charAt(curr); + if(ch == '\\' || ch == ';') + { + sb.append('\\'); + } + sb.append(ch); + } + return sb.toString(); + } + + // Obtain the Ice version by executing the translator with the -v option. + private String getIceVersion() + { + String version = null; + String exec = getTranslatorForHome(getIceHome()); + if(exec != null) + { + try + { + ProcessBuilder b = new ProcessBuilder(exec, "-v"); + b.redirectErrorStream(true); + Map<String, String> env = b.environment(); + setupSharedLibraryPath(env); + Process p = b.start(); + int status = p.waitFor(); + if(status == 0) + { + BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream())); + String line = r.readLine(); + version = line.trim(); + } + } + catch(Throwable ex) + { + // Ignore. + } + } + return version; + } + + private static String getTranslatorForHome(String dir) + { + String suffix = ""; + String os = System.getProperty("os.name"); + if(os.startsWith("Windows")) + { + suffix = ".exe"; + } + File f = new File(dir + File.separator + "bin" + File.separator + "slice2java" + suffix); + if(f.exists()) + { + return f.toString(); + } + f = new File(dir + File.separator + "cpp" + File.separator + "bin" + File.separator + "slice2java" + suffix); + if(f.exists()) + { + return f.toString(); + } + return null; + } + + private static final String JARS_KEY = "jars"; + private static final String INCLUDES_KEY = "includes"; + private static final String SLICE_SOURCE_DIRS_KEY = "sliceSourceDirs"; + private static final String CONSOLE_KEY = "console"; + private static final String META_KEY = "meta"; + private static final String STREAM_KEY = "stream"; + private static final String ICE_INCLUDE_KEY = "iceIncludes"; + private static final String ICE_KEY = "ice"; + private static final String TIE_KEY = "tie"; + private static final String DEFINES_KEY = "defines"; + private static final String GENERATED_KEY = "generated"; + + // Preferences store for items which should go in SCM. This includes things + // like build flags. + private ScopedPreferenceStore _store; + + // Preferences store per project items which should not go in SCM, such as + // the location of the Ice installation. + private ScopedPreferenceStore _instanceStore; + + private IProject _project; + +} diff --git a/eclipse/Slice2javaPlugin/src/com/zeroc/slice2javaplugin/internal/Dependencies.java b/eclipse/Slice2javaPlugin/src/com/zeroc/slice2javaplugin/internal/Dependencies.java new file mode 100644 index 00000000000..033f7f7601c --- /dev/null +++ b/eclipse/Slice2javaPlugin/src/com/zeroc/slice2javaplugin/internal/Dependencies.java @@ -0,0 +1,534 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved. +// +// This plug-in is provided to you under the terms and conditions +// of the Eclipse Public License Version 1.0 ("EPL"). A copy of +// the EPL is available at http://www.eclipse.org/legal/epl-v10.html. +// +// ********************************************************************** + +package com.zeroc.slice2javaplugin.internal; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileStore; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Status; +import org.eclipse.ui.console.MessageConsoleStream; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.Text; +import org.xml.sax.SAXException; + +import com.zeroc.slice2javaplugin.Activator; + +public class Dependencies +{ + public Dependencies(IProject project, Set<IFile> LprojectResources, MessageConsoleStream err) + { + _project = project; + _projectResources = LprojectResources; + _err = err; + + // Build a map of location to project resource. + + for(Iterator<IFile> p = _projectResources.iterator(); p.hasNext();) + { + IFile f = p.next(); + _locationToResource.put(f.getLocation(), f); + } + } + + /** + * + * @param allDependencies The string of all dependencies. + * @param _projectResources a set of all slice file project resources. + * @throws CoreException + */ + public void updateDependencies(String allDependencies) + throws CoreException + { + Slice2JavaDependenciesParser parser = new Slice2JavaDependenciesParser(); + try + { + InputStream in = new ByteArrayInputStream(allDependencies.getBytes()); + Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new BufferedInputStream(in)); + parser.visit(doc); + } + catch(SAXException e) + { + throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "internal error reading dependencies", e)); + } + catch(ParserConfigurationException e) + { + throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "internal error reading dependencies", e)); + } + catch(IOException e) + { + throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "internal error reading dependencies", e)); + } + + for(Iterator<Map.Entry<String, List<String>>> p = parser.dependencies.entrySet().iterator(); p.hasNext();) + { + Map.Entry<String, List<String>> entry = p.next(); + + Path sourcePath = new Path(entry.getKey()); + assert sourcePath.isAbsolute(); + + IFile sourceFile = _locationToResource.get(sourcePath); + if(sourceFile == null) + { + if(_err != null) + { + _err.println("Dependencies: ignoring non-project resource " + sourcePath.toString()); + } + // This should not occur. + continue; + } + + for(Iterator<String> q = entry.getValue().iterator(); q.hasNext();) + { + IFile f = getProjectResource(new Path(q.next())); + // Ignore any resources not in the project. + if(f != null) + { + Set<IFile> dependents = reverseSliceSliceDependencies.get(f); + if(dependents == null) + { + dependents = new HashSet<IFile>(); + reverseSliceSliceDependencies.put(f, dependents); + } + dependents.add(sourceFile); + } + } + + Set<IFile> dependents = new HashSet<IFile>(); + sliceSliceDependencies.put(sourceFile, dependents); + for(Iterator<String> q = entry.getValue().iterator(); q.hasNext();) + { + IFile f = getProjectResource(new Path(q.next())); + // Ignore any resources not in the project. + if(f != null) + { + dependents.add(f); + } + } + } + } + + private IFile getProjectResource(Path path) + { + IFile f = null; + if(path.isAbsolute()) + { + f = _locationToResource.get(path); + } + else + { + IStatus s = _project.getWorkspace().validatePath(path.toString(), IResource.FILE); + if(s.getCode() == IStatus.OK) + { + f = _project.getFile(path); + } + } + if(_projectResources.contains(f)) + { + return f; + } + return null; + } + + public void read() + throws CoreException + { + IFileStore dependencies = getDependenciesStore(); + if(!dependencies.fetchInfo(EFS.NONE, null).exists()) + { + return; + } + InputStream in = dependencies.openInputStream(EFS.NONE, null); + + try + { + Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new BufferedInputStream(in)); + DependenciesParser parser = new DependenciesParser(_project); + parser.visit(doc); + sliceSliceDependencies = parser.sliceSliceDependencies; + reverseSliceSliceDependencies = parser.reverseSliceSliceDependencies; + sliceJavaDependencies = parser.sliceJavaDependencies; + errorSliceFiles = parser.errorSliceFiles; + } + catch(SAXException e) + { + throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "internal error reading dependencies", e)); + } + catch(ParserConfigurationException e) + { + throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "internal error reading dependencies", e)); + } + catch(IOException e) + { + throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "internal error reading dependencies", e)); + } + } + + public void write() + throws CoreException + { + // Create a DOM of the map. + Document doc = null; + try + { + doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); + } + catch(ParserConfigurationException e) + { + throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "internal error writing dependencies", e)); + } + + Element root = doc.createElement("dependencies"); + doc.appendChild(root); + + writeDependencies(sliceSliceDependencies, doc, "sliceSliceDependencies", root); + writeDependencies(reverseSliceSliceDependencies, doc, "reverseSliceSliceDependencies", root); + writeDependencies(sliceJavaDependencies, doc, "sliceJavaDependencies", root); + writeErrorSliceFiles(errorSliceFiles, doc, "errorSliceFiles", root); + + // Write the DOM to the dependencies.xml file. + TransformerFactory transfac = TransformerFactory.newInstance(); + Transformer trans = null; + try + { + trans = transfac.newTransformer(); + } + catch(TransformerConfigurationException e) + { + throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "internal error writing dependencies", e)); + } + // tf.setAttribute("indent-number", 4); + + // trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + trans.setOutputProperty(OutputKeys.INDENT, "yes"); + trans.setOutputProperty(OutputKeys.ENCODING, "UTF8"); + trans.setOutputProperty(OutputKeys.INDENT, "yes"); + trans.setOutputProperty(OutputKeys.METHOD, "XML"); + + IFileStore dependencies = getDependenciesStore(); + OutputStream out = dependencies.openOutputStream(EFS.NONE, null); + StreamResult result = new StreamResult(new BufferedOutputStream(out)); + DOMSource source = new DOMSource(doc); + try + { + trans.transform(source, result); + } + catch(TransformerException e) + { + throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "internal error writing dependencies", e)); + } + try + { + out.close(); + } + catch(IOException e) + { + throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "internal error writing dependencies", e)); + } + } + + private void writeErrorSliceFiles(Set<IFile> s, Document doc, String name, Element root) + { + Element jsd = doc.createElement(name); + root.appendChild(jsd); + + for(IFile f : s) + { + Element elem = doc.createElement("file"); + jsd.appendChild(elem); + Text text = doc.createTextNode(f.getProjectRelativePath().toString()); + elem.appendChild(text); + } + } + + private void writeDependencies(Map<IFile, Set<IFile>> map, Document doc, String name, Element root) + { + Element jsd = doc.createElement(name); + root.appendChild(jsd); + + Iterator<Map.Entry<IFile, Set<IFile>>> p = map.entrySet().iterator(); + while(p.hasNext()) + { + Map.Entry<IFile, Set<IFile>> e = p.next(); + Element entry = doc.createElement("entry"); + jsd.appendChild(entry); + + Element key = doc.createElement("key"); + entry.appendChild(key); + Text text = doc.createTextNode(e.getKey().getProjectRelativePath().toString()); + key.appendChild(text); + + Element value = doc.createElement("value"); + entry.appendChild(value); + + Iterator<IFile> q = e.getValue().iterator(); + while(q.hasNext()) + { + IFile f = q.next(); + Element elem = doc.createElement("file"); + value.appendChild(elem); + text = doc.createTextNode(f.getProjectRelativePath().toString()); + elem.appendChild(text); + } + } + } + + private IFileStore getDependenciesStore() + throws CoreException + { + IPath name = new Path(_project.getName()); + IFileStore store = EFS.getLocalFileSystem().getStore(Activator.getDefault().getStateLocation()).getFileStore( + name); + if(!store.fetchInfo(EFS.NONE, null).exists()) + { + store.mkdir(EFS.NONE, null); + } + return store.getFileStore(new Path("dependencies.xml")); + } + + private static class Slice2JavaDependenciesParser + { + Map<String, List<String>> dependencies = new java.util.HashMap<String, List<String>>(); + + private Node findNode(Node n, String qName) + throws SAXException + { + NodeList children = n.getChildNodes(); + for(int i = 0; i < children.getLength(); ++i) + { + Node child = children.item(i); + if(child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName().equals(qName)) + { + return child; + } + } + throw new SAXException("no such node: " + qName); + } + + private void visitDependencies(Node n) throws SAXException + { + NodeList children = n.getChildNodes(); + for(int i = 0; i < children.getLength(); ++i) + { + if(children.item(i).getNodeType() == Node.ELEMENT_NODE && children.item(i).getNodeName().equals("source")) + { + String source = ((Element)children.item(i)).getAttribute("name"); + if(source.length() == 0) + { + throw new SAXException("empty name attribute"); + } + List<String> dependsOn = visitDependsOn(children.item(i)); + dependencies.put(source, dependsOn); + } + } + } + + private List<String> visitDependsOn(Node source) throws SAXException + { + List<String> depends = new ArrayList<String>(); + NodeList dependencies = source.getChildNodes(); + for(int j = 0; j < dependencies.getLength(); ++j) + { + if(dependencies.item(j).getNodeType() == Node.ELEMENT_NODE && dependencies.item(j).getNodeName().equals("dependsOn")) + { + Element dependsOn = (Element)dependencies.item(j); + String name = dependsOn.getAttribute("name"); + if(name.length() == 0) + { + throw new SAXException("empty name attribute"); + } + depends.add(name); + } + } + return depends; + } + + public void visit(Node doc) throws SAXException + { + Node n = findNode(doc, "dependencies"); + visitDependencies(n); + } + } + + private static class DependenciesParser + { + private IProject _project; + + Map<IFile, Set<IFile>> sliceSliceDependencies = new java.util.HashMap<IFile, Set<IFile>>(); + Map<IFile, Set<IFile>> reverseSliceSliceDependencies = new java.util.HashMap<IFile, Set<IFile>>(); + Map<IFile, Set<IFile>> sliceJavaDependencies = new java.util.HashMap<IFile, Set<IFile>>(); + Set<IFile> errorSliceFiles = new java.util.HashSet<IFile>(); + + private Node findNode(Node n, String qName) + throws SAXException + { + NodeList children = n.getChildNodes(); + for(int i = 0; i < children.getLength(); ++i) + { + Node child = children.item(i); + if(child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName().equals(qName)) + { + return child; + } + } + throw new SAXException("no such node: " + qName); + } + + private String getText(Node n) + throws SAXException + { + NodeList children = n.getChildNodes(); + if(children.getLength() == 1 && children.item(0).getNodeType() == Node.TEXT_NODE) + { + return children.item(0).getNodeValue(); + } + throw new SAXException("no text element"); + } + + private List<String> processFiles(Node n) + throws SAXException + { + List<String> files = new ArrayList<String>(); + NodeList children = n.getChildNodes(); + for(int i = 0; i < children.getLength(); ++i) + { + Node child = children.item(i); + if(child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName().equals("file")) + { + files.add(getText(child)); + } + } + return files; + } + + public void visitDependencies(Map<IFile, Set<IFile>> map, Node n) + throws SAXException + { + NodeList children = n.getChildNodes(); + for(int i = 0; i < children.getLength(); ++i) + { + Node child = children.item(i); + if(child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName().equals("entry")) + { + IFile key = _project.getFile(new Path(getText(findNode(child, "key")))); + + Node value = findNode(child, "value"); + List<String> files = processFiles(value); + Set<IFile> f = new HashSet<IFile>(); + Iterator<String> p = files.iterator(); + while(p.hasNext()) + { + f.add(_project.getFile(new Path(p.next()))); + } + + map.put(key, f); + } + } + } + + public void visitErrorList(Set<IFile> s, Node n) throws SAXException + { + NodeList children = n.getChildNodes(); + for(int i = 0; i < children.getLength(); ++i) + { + Node child = children.item(i); + if(child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName().equals("file")) + { + s.add(_project.getFile(new Path(getText(child)))); + } + } + } + + public void visit(Node doc) + throws SAXException + { + Node dependencies = findNode(doc, "dependencies"); + visitDependencies(sliceSliceDependencies, findNode(dependencies, "sliceSliceDependencies")); + visitDependencies(reverseSliceSliceDependencies, findNode(dependencies, "reverseSliceSliceDependencies")); + visitDependencies(sliceJavaDependencies, findNode(dependencies, "sliceJavaDependencies")); + try + { + visitErrorList(errorSliceFiles, findNode(dependencies, "errorSliceFiles")); + } + catch(SAXException e) + { + // Optional. + } + } + + DependenciesParser(IProject project) + { + _project = project; + } + } + + // A map of slice to dependencies. + // + // sliceSliceDependencies is the set of slice files that depend on the IFile + // (the output of slice2java --depend). + // + // _reverseSliceSliceDependencies is the reverse. + public Map<IFile, Set<IFile>> sliceSliceDependencies = new java.util.HashMap<IFile, Set<IFile>>(); + public Map<IFile, Set<IFile>> reverseSliceSliceDependencies = new java.util.HashMap<IFile, Set<IFile>>(); + + // A map of slice file to java source files. + public Map<IFile, Set<IFile>> sliceJavaDependencies = new java.util.HashMap<IFile, Set<IFile>>(); + + // A set of slice files that have not, or cannot be built. + public Set<IFile> errorSliceFiles = new java.util.HashSet<IFile>(); + + private IProject _project; + private MessageConsoleStream _err; + private Set<IFile> _projectResources; + private Map<IPath, IFile> _locationToResource = new HashMap<IPath, IFile>(); +} diff --git a/eclipse/Slice2javaPlugin/src/com/zeroc/slice2javaplugin/internal/IceClasspathContainer.java b/eclipse/Slice2javaPlugin/src/com/zeroc/slice2javaplugin/internal/IceClasspathContainer.java new file mode 100644 index 00000000000..31bdf060054 --- /dev/null +++ b/eclipse/Slice2javaPlugin/src/com/zeroc/slice2javaplugin/internal/IceClasspathContainer.java @@ -0,0 +1,47 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved. +// +// This plug-in is provided to you under the terms and conditions +// of the Eclipse Public License Version 1.0 ("EPL"). A copy of +// the EPL is available at http://www.eclipse.org/legal/epl-v10.html. +// +// ********************************************************************** + +package com.zeroc.slice2javaplugin.internal; + +import org.eclipse.core.runtime.IPath; +import org.eclipse.jdt.core.IClasspathContainer; +import org.eclipse.jdt.core.IClasspathEntry; + +public class IceClasspathContainer implements IClasspathContainer +{ + private IClasspathEntry[] _cpEntry; + private IPath _path; + + IceClasspathContainer(IClasspathEntry[] entries, IPath path) + { + _cpEntry = entries; + _path = path; + } + + public IClasspathEntry[] getClasspathEntries() + { + return _cpEntry; + } + + public String getDescription() + { + return "Ice Library"; + } + + public int getKind() + { + return IClasspathContainer.K_DEFAULT_SYSTEM; + } + + public IPath getPath() + { + return _path; + } +} diff --git a/eclipse/Slice2javaPlugin/src/com/zeroc/slice2javaplugin/internal/IceClasspathContainerIntializer.java b/eclipse/Slice2javaPlugin/src/com/zeroc/slice2javaplugin/internal/IceClasspathContainerIntializer.java new file mode 100644 index 00000000000..bd4933b6706 --- /dev/null +++ b/eclipse/Slice2javaPlugin/src/com/zeroc/slice2javaplugin/internal/IceClasspathContainerIntializer.java @@ -0,0 +1,93 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved. +// +// This plug-in is provided to you under the terms and conditions +// of the Eclipse Public License Version 1.0 ("EPL"). A copy of +// the EPL is available at http://www.eclipse.org/legal/epl-v10.html. +// +// ********************************************************************** + +package com.zeroc.slice2javaplugin.internal; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Path; +import org.eclipse.jdt.core.ClasspathContainerInitializer; +import org.eclipse.jdt.core.IAccessRule; +import org.eclipse.jdt.core.IClasspathAttribute; +import org.eclipse.jdt.core.IClasspathContainer; +import org.eclipse.jdt.core.IClasspathEntry; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.JavaModelException; + +public class IceClasspathContainerIntializer extends ClasspathContainerInitializer +{ + private final static String CONTAINER_ID = "com.zeroc.Slice2JavaPlugin.ICE_FRAMEWORK"; + + @Override + public void initialize(IPath containerPath, IJavaProject project) + throws CoreException + { + if(containerPath.toString().equals(CONTAINER_ID)) + { + Configuration c = new Configuration(project.getProject()); + configure(c, project, containerPath); + } + } + + public static IClasspathEntry getContainerEntry() + { + return JavaCore.newContainerEntry(new Path(CONTAINER_ID)); + } + + public static void reinitialize(IProject _project, Configuration c) + throws CoreException + { + IJavaProject javaProject = JavaCore.create(_project); + IPath containerPath = new Path(CONTAINER_ID); + + configure(c, javaProject, containerPath); + } + + private static void configure(Configuration c, IJavaProject javaProject, IPath containerPath) + throws JavaModelException + { + Path dir = new Path(c.getJarDir()); + List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>(); + for(String jar : c.getJars()) + { + IPath path = dir.append(new Path(jar)); + IClasspathEntry classpathEntry = JavaCore.newLibraryEntry(path, null, null, new IAccessRule[0], new IClasspathAttribute[0], false); + entries.add(classpathEntry); + } + + IClasspathContainer container = new IceClasspathContainer(entries.toArray(new IClasspathEntry[0]), containerPath); + JavaCore.setClasspathContainer(containerPath, new IJavaProject[] { javaProject }, + new IClasspathContainer[] { container }, new NullProgressMonitor()); + } + + public static void updateProjects(String value, List<IJavaProject> projects) + { + for(IJavaProject p : projects) + { + IPath containerPath = new Path(CONTAINER_ID); + Configuration c = new Configuration(p.getProject()); + try + { + configure(c, p, containerPath); + } + catch(JavaModelException e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } +} |