Search in sources :

Example 6 with XMLMemento

use of org.eclipse.wst.server.core.internal.XMLMemento 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 7 with XMLMemento

use of org.eclipse.wst.server.core.internal.XMLMemento 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)

Aggregations

XMLMemento (org.eclipse.wst.server.core.internal.XMLMemento)7 File (java.io.File)5 IMemento (org.eclipse.wst.server.core.internal.IMemento)5 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 IPath (org.eclipse.core.runtime.IPath)4 OutputStream (java.io.OutputStream)3 HashSet (java.util.HashSet)3 HashMap (java.util.HashMap)2 Path (org.eclipse.core.runtime.Path)2 IServer (org.eclipse.wst.server.core.IServer)2 BackingStoreException (org.osgi.service.prefs.BackingStoreException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 URL (java.net.URL)1 IRuntime (org.eclipse.wst.server.core.IRuntime)1 IRuntimeType (org.eclipse.wst.server.core.IRuntimeType)1 IServerType (org.eclipse.wst.server.core.IServerType)1 Server (org.eclipse.wst.server.core.internal.Server)1 Bundle (org.osgi.framework.Bundle)1