Search in sources :

Example 6 with IMemento

use of org.eclipse.wst.server.core.internal.IMemento in project webtools.servertools by eclipse.

the class ServerTransfer method javaToNative.

/* (non-Javadoc)
	 * @see org.eclipse.swt.dnd.Transfer#javaToNative(java.lang.Object, org.eclipse.swt.dnd.TransferData)
	 */
protected void javaToNative(Object data, TransferData transferData) {
    if (!(data instanceof IServer[]))
        return;
    IServer[] servers = (IServer[]) data;
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        XMLMemento memento = XMLMemento.createWriteRoot("servers");
        // write each element
        for (int i = 0; i < servers.length; i++) {
            IMemento child = memento.createChild("server");
            ((Server) servers[i]).serialize(child);
        }
        memento.save(out);
        // cleanup
        out.close();
        byte[] bytes = out.toByteArray();
        super.javaToNative(bytes, transferData);
    } catch (IOException e) {
    // it's best to send nothing if there were problems
    }
}
Also used : IServer(org.eclipse.wst.server.core.IServer) XMLMemento(org.eclipse.wst.server.core.internal.XMLMemento) Server(org.eclipse.wst.server.core.internal.Server) IServer(org.eclipse.wst.server.core.IServer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IMemento(org.eclipse.wst.server.core.internal.IMemento)

Example 7 with IMemento

use of org.eclipse.wst.server.core.internal.IMemento in project liferay-ide by liferay.

the class SDKCorePlugin method _saveGlobalSDKSettings.

private synchronized void _saveGlobalSDKSettings(SDK[] sdks) {
    try {
        LiferayCore.GLOBAL_SETTINGS_PATH.toFile().mkdirs();
        File sdkGlobalFile = LiferayCore.GLOBAL_SETTINGS_PATH.append("sdks.xml").toFile();
        Set<IMemento> existing = new HashSet<>();
        if (sdkGlobalFile.exists()) {
            try {
                try (InputStream newInputStream = Files.newInputStream(sdkGlobalFile.toPath())) {
                    IMemento existingMemento = XMLMemento.loadMemento(newInputStream);
                    if (existingMemento != null) {
                        IMemento[] children = existingMemento.getChildren("sdk");
                        if (ListUtil.isNotEmpty(children)) {
                            for (IMemento child : children) {
                                IPath loc = Path.fromPortableString(child.getString("location"));
                                if ((loc != null) && loc.toFile().exists()) {
                                    boolean duplicate = false;
                                    for (SDK sdk : sdks) {
                                        IPath sdkLocation = sdk.getLocation();
                                        if (sdkLocation.toFile().equals(loc.toFile())) {
                                            duplicate = true;
                                            break;
                                        }
                                    }
                                    if (!duplicate) {
                                        existing.add(child);
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
            }
        }
        XMLMemento sdkMementos = XMLMemento.createWriteRoot("sdks");
        for (IMemento exist : existing) {
            _copyMemento(exist, sdkMementos.createChild("sdk"));
        }
        for (SDK sdk : sdks) {
            IMemento memento = sdkMementos.createChild("sdk");
            _addSDKToMemento(sdk, memento);
        }
        OutputStream fos = Files.newOutputStream(sdkGlobalFile.toPath());
        sdkMementos.save(fos);
    } catch (Exception e) {
        logError("Unable to save global sdk settings", e);
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) XMLMemento(org.eclipse.wst.server.core.internal.XMLMemento) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) File(java.io.File) IMemento(org.eclipse.wst.server.core.internal.IMemento) HashSet(java.util.HashSet)

Example 8 with IMemento

use of org.eclipse.wst.server.core.internal.IMemento in project liferay-ide by liferay.

the class LiferayServerCore method saveGlobalRuntimeSettings.

private synchronized void saveGlobalRuntimeSettings(IRuntime runtime) {
    final IRuntimeType runtimeType = runtime.getRuntimeType();
    if (runtimeType != null && runtimeType.getId().startsWith("com.liferay")) {
        try {
            LiferayCore.GLOBAL_SETTINGS_PATH.toFile().mkdirs();
            final File runtimesGlobalFile = LiferayCore.GLOBAL_SETTINGS_PATH.append("runtimes.xml").toFile();
            final Set<IMemento> existing = new HashSet<IMemento>();
            if (runtimesGlobalFile.exists()) {
                try {
                    try (InputStream newInputStream = Files.newInputStream(runtimesGlobalFile.toPath())) {
                        IMemento existingMemento = XMLMemento.loadMemento(newInputStream);
                        if (existingMemento != null) {
                            IMemento[] children = existingMemento.getChildren("runtime");
                            if (ListUtil.isNotEmpty(children)) {
                                for (IMemento child : children) {
                                    final IPath loc = Path.fromPortableString(child.getString("location"));
                                    if (loc != null && loc.toFile().exists()) {
                                        boolean duplicate = ServerCore.findRuntime(child.getString("id")) != null;
                                        if (!duplicate) {
                                            existing.add(child);
                                        }
                                    }
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                }
            }
            final Map<String, IMemento> mementos = new HashMap<String, IMemento>();
            final XMLMemento runtimeMementos = XMLMemento.createWriteRoot("runtimes");
            for (IMemento exist : existing) {
                final IMemento copy = runtimeMementos.createChild("runtime");
                copyMemento(exist, copy);
                mementos.put(copy.getString("id"), copy);
            }
            for (IRuntime r : ServerCore.getRuntimes()) {
                if (mementos.get(r.getId()) == null && r.getRuntimeType() != null) {
                    final IMemento rMemento = runtimeMementos.createChild("runtime");
                    if (addRuntimeToMemento(r, rMemento)) {
                        mementos.put(r.getId(), rMemento);
                    }
                }
            }
            final OutputStream fos = Files.newOutputStream(runtimesGlobalFile.toPath());
            runtimeMementos.save(fos);
        } catch (Exception e) {
            LiferayServerCore.logError("Unable to save global runtime settings", e);
        }
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) HashMap(java.util.HashMap) IRuntimeType(org.eclipse.wst.server.core.IRuntimeType) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IMemento(org.eclipse.wst.server.core.internal.IMemento) BackingStoreException(org.osgi.service.prefs.BackingStoreException) IOException(java.io.IOException) IRuntime(org.eclipse.wst.server.core.IRuntime) XMLMemento(org.eclipse.wst.server.core.internal.XMLMemento) File(java.io.File) HashSet(java.util.HashSet)

Example 9 with IMemento

use of org.eclipse.wst.server.core.internal.IMemento in project liferay-ide by liferay.

the class LiferayServerCore method saveGlobalServerSettings.

private synchronized void saveGlobalServerSettings(IServer server) {
    final IServerType serverType = server.getServerType();
    if (serverType != null && serverType.getId().startsWith("com.liferay")) {
        try {
            LiferayCore.GLOBAL_SETTINGS_PATH.toFile().mkdirs();
            final File globalServersFile = LiferayCore.GLOBAL_SETTINGS_PATH.append("servers.xml").toFile();
            final Set<IMemento> existing = new HashSet<IMemento>();
            if (globalServersFile.exists()) {
                try {
                    try (InputStream newInputStream = Files.newInputStream(globalServersFile.toPath())) {
                        IMemento existingMemento = XMLMemento.loadMemento(newInputStream);
                        if (existingMemento != null) {
                            IMemento[] children = existingMemento.getChildren("server");
                            if (ListUtil.isNotEmpty(children)) {
                                for (IMemento child : children) {
                                    final boolean duplicate = ServerCore.findServer(child.getString("id")) != null;
                                    if (!duplicate) {
                                        existing.add(child);
                                    }
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                }
            }
            final Map<String, IMemento> mementos = new HashMap<String, IMemento>();
            final XMLMemento serverMementos = XMLMemento.createWriteRoot("servers");
            for (IMemento exist : existing) {
                final IMemento copy = serverMementos.createChild("server");
                copyMemento(exist, copy);
                mementos.put(copy.getString("id"), copy);
            }
            for (IServer s : ServerCore.getServers()) {
                if (mementos.get(s.getId()) == null && s.getServerType() != null) {
                    final IMemento sMemento = serverMementos.createChild("server");
                    if (addServerToMemento(s, sMemento)) {
                        mementos.put(s.getId(), sMemento);
                    }
                }
            }
            if (mementos.size() > 0) {
                final OutputStream fos = Files.newOutputStream(globalServersFile.toPath());
                serverMementos.save(fos);
            }
        } catch (Exception e) {
            LiferayServerCore.logError("Unable to save global server settings", e);
        }
    }
}
Also used : IServer(org.eclipse.wst.server.core.IServer) HashMap(java.util.HashMap) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IMemento(org.eclipse.wst.server.core.internal.IMemento) BackingStoreException(org.osgi.service.prefs.BackingStoreException) IOException(java.io.IOException) XMLMemento(org.eclipse.wst.server.core.internal.XMLMemento) IServerType(org.eclipse.wst.server.core.IServerType) File(java.io.File) HashSet(java.util.HashSet)

Example 10 with IMemento

use of org.eclipse.wst.server.core.internal.IMemento in project liferay-ide by liferay.

the class ServerStartup method importGlobalRuntimes.

private void importGlobalRuntimes(File runtimesFile) {
    try {
        final IMemento runtimesMemento = XMLMemento.loadMemento(new FileInputStream(runtimesFile));
        if (runtimesMemento != null) {
            final ResourceManager resourceManager = ResourceManager.getInstance();
            final IMemento[] mementos = runtimesMemento.getChildren("runtime");
            if (ListUtil.isNotEmpty(mementos)) {
                for (IMemento memento : mementos) {
                    final Runtime runtime = new Runtime(null);
                    try {
                        final Method loadFromMemento = Base.class.getDeclaredMethod("loadFromMemento", IMemento.class, IProgressMonitor.class);
                        if (loadFromMemento != null) {
                            loadFromMemento.setAccessible(true);
                            loadFromMemento.invoke(runtime, memento, null);
                            if (ServerCore.findRuntime(runtime.getId()) == null) {
                                final Method addRuntime = ResourceManager.class.getDeclaredMethod("addRuntime", IRuntime.class);
                                if (addRuntime != null) {
                                    addRuntime.setAccessible(true);
                                    addRuntime.invoke(resourceManager, runtime);
                                }
                            }
                        }
                    } catch (Exception e) {
                        LiferayServerUI.logError("Unable to load runtime from memento", e);
                    }
                }
            }
        }
    } catch (FileNotFoundException e) {
    }
}
Also used : Runtime(org.eclipse.wst.server.core.internal.Runtime) IRuntime(org.eclipse.wst.server.core.IRuntime) FileNotFoundException(java.io.FileNotFoundException) ResourceManager(org.eclipse.wst.server.core.internal.ResourceManager) Method(java.lang.reflect.Method) IMemento(org.eclipse.wst.server.core.internal.IMemento) FileInputStream(java.io.FileInputStream) BackingStoreException(org.osgi.service.prefs.BackingStoreException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

IMemento (org.eclipse.wst.server.core.internal.IMemento)10 IOException (java.io.IOException)5 XMLMemento (org.eclipse.wst.server.core.internal.XMLMemento)5 InputStream (java.io.InputStream)4 IServer (org.eclipse.wst.server.core.IServer)4 BackingStoreException (org.osgi.service.prefs.BackingStoreException)4 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 OutputStream (java.io.OutputStream)3 HashSet (java.util.HashSet)3 Server (org.eclipse.wst.server.core.internal.Server)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 IPath (org.eclipse.core.runtime.IPath)2 IRuntime (org.eclipse.wst.server.core.IRuntime)2 ResourceManager (org.eclipse.wst.server.core.internal.ResourceManager)2 SDK (com.liferay.ide.sdk.core.SDK)1 SDKManager (com.liferay.ide.sdk.core.SDKManager)1