Search in sources :

Example 1 with JarDeployer

use of org.apache.geode.internal.JarDeployer in project geode by apache.

the class DeployFunction method execute.

@Override
public void execute(FunctionContext context) {
    // Declared here so that it's available when returning a Throwable
    String memberId = "";
    try {
        final Object[] args = (Object[]) context.getArguments();
        final String[] jarFilenames = (String[]) args[0];
        final byte[][] jarBytes = (byte[][]) args[1];
        InternalCache cache = getCache();
        final JarDeployer jarDeployer = new JarDeployer(cache.getInternalDistributedSystem().getConfig().getDeployWorkingDir());
        DistributedMember member = cache.getDistributedSystem().getDistributedMember();
        memberId = member.getId();
        // If they set a name use it instead
        if (!member.getName().equals("")) {
            memberId = member.getName();
        }
        List<String> deployedList = new ArrayList<String>();
        List<DeployedJar> jarClassLoaders = ClassPathLoader.getLatest().getJarDeployer().deploy(jarFilenames, jarBytes);
        for (int i = 0; i < jarFilenames.length; i++) {
            deployedList.add(jarFilenames[i]);
            if (jarClassLoaders.get(i) != null) {
                deployedList.add(jarClassLoaders.get(i).getFileCanonicalPath());
            } else {
                deployedList.add("Already deployed");
            }
        }
        CliFunctionResult result = new CliFunctionResult(memberId, deployedList.toArray(new String[0]));
        context.getResultSender().lastResult(result);
    } catch (CacheClosedException cce) {
        CliFunctionResult result = new CliFunctionResult(memberId, false, null);
        context.getResultSender().lastResult(result);
    } catch (VirtualMachineError e) {
        SystemFailure.initiateFailure(e);
        throw e;
    } catch (Throwable th) {
        SystemFailure.checkFailure();
        logger.error("Could not deploy JAR file {}", th.getMessage(), th);
        CliFunctionResult result = new CliFunctionResult(memberId, th, null);
        context.getResultSender().lastResult(result);
    }
}
Also used : DeployedJar(org.apache.geode.internal.DeployedJar) ArrayList(java.util.ArrayList) InternalCache(org.apache.geode.internal.cache.InternalCache) CacheClosedException(org.apache.geode.cache.CacheClosedException) DistributedMember(org.apache.geode.distributed.DistributedMember) JarDeployer(org.apache.geode.internal.JarDeployer)

Example 2 with JarDeployer

use of org.apache.geode.internal.JarDeployer in project geode by apache.

the class ListDeployedFunction method execute.

@Override
public void execute(FunctionContext context) {
    // Declared here so that it's available when returning a Throwable
    String memberId = "";
    try {
        InternalCache cache = getCache();
        final JarDeployer jarDeployer = ClassPathLoader.getLatest().getJarDeployer();
        DistributedMember member = cache.getDistributedSystem().getDistributedMember();
        memberId = member.getId();
        // If they set a name use it instead
        if (!member.getName().equals("")) {
            memberId = member.getName();
        }
        final List<DeployedJar> jarClassLoaders = jarDeployer.findDeployedJars();
        final String[] jars = new String[jarClassLoaders.size() * 2];
        int index = 0;
        for (DeployedJar jarClassLoader : jarClassLoaders) {
            jars[index++] = jarClassLoader.getJarName();
            jars[index++] = jarClassLoader.getFileCanonicalPath();
        }
        CliFunctionResult result = new CliFunctionResult(memberId, jars);
        context.getResultSender().lastResult(result);
    } catch (CacheClosedException cce) {
        CliFunctionResult result = new CliFunctionResult(memberId, false, null);
        context.getResultSender().lastResult(result);
    } catch (VirtualMachineError e) {
        SystemFailure.initiateFailure(e);
        throw e;
    } catch (Throwable th) {
        SystemFailure.checkFailure();
        logger.error("Could not list JAR files: {}", th.getMessage(), th);
        CliFunctionResult result = new CliFunctionResult(memberId, th, null);
        context.getResultSender().lastResult(result);
    }
}
Also used : DeployedJar(org.apache.geode.internal.DeployedJar) DistributedMember(org.apache.geode.distributed.DistributedMember) InternalCache(org.apache.geode.internal.cache.InternalCache) CacheClosedException(org.apache.geode.cache.CacheClosedException) JarDeployer(org.apache.geode.internal.JarDeployer)

Example 3 with JarDeployer

use of org.apache.geode.internal.JarDeployer in project geode by apache.

the class BackupManager method backupDeployedJars.

/**
   * Copies user deployed jars to the backup directory.
   * 
   * @param restoreScript Used to restore from this backup.
   * @param backupDir The backup directory for this member.
   * @throws IOException one or more of the jars did not successfully copy.
   */
private void backupDeployedJars(RestoreScript restoreScript, File backupDir) throws IOException {
    JarDeployer deployer = null;
    try {
        /*
       * Suspend any user deployed jar file updates during this backup.
       */
        deployer = ClassPathLoader.getLatest().getJarDeployer();
        deployer.suspendAll();
        List<DeployedJar> jarList = deployer.findDeployedJars();
        if (!jarList.isEmpty()) {
            File userBackupDir = new File(backupDir, USER_FILES);
            if (!userBackupDir.exists()) {
                userBackupDir.mkdir();
            }
            for (DeployedJar loader : jarList) {
                File source = new File(loader.getFileCanonicalPath());
                File dest = new File(userBackupDir, source.getName());
                if (source.isDirectory()) {
                    FileUtils.copyDirectory(source, dest);
                } else {
                    FileUtils.copyFile(source, dest);
                }
                restoreScript.addFile(source, dest);
            }
        }
    } finally {
        /*
       * Re-enable user deployed jar file updates.
       */
        if (null != deployer) {
            deployer.resumeAll();
        }
    }
}
Also used : DeployedJar(org.apache.geode.internal.DeployedJar) JarDeployer(org.apache.geode.internal.JarDeployer) File(java.io.File)

Example 4 with JarDeployer

use of org.apache.geode.internal.JarDeployer in project geode by apache.

the class UndeployFunction method execute.

@Override
public void execute(FunctionContext context) {
    // Declared here so that it's available when returning a Throwable
    String memberId = "";
    try {
        final Object[] args = (Object[]) context.getArguments();
        // Comma separated
        final String jarFilenameList = (String) args[0];
        InternalCache cache = getCache();
        final JarDeployer jarDeployer = ClassPathLoader.getLatest().getJarDeployer();
        DistributedMember member = cache.getDistributedSystem().getDistributedMember();
        memberId = member.getId();
        // If they set a name use it instead
        if (!member.getName().equals("")) {
            memberId = member.getName();
        }
        String[] undeployedJars = new String[0];
        if (jarFilenameList == null || jarFilenameList.equals("")) {
            final List<DeployedJar> jarClassLoaders = jarDeployer.findDeployedJars();
            undeployedJars = new String[jarClassLoaders.size() * 2];
            int index = 0;
            for (DeployedJar jarClassLoader : jarClassLoaders) {
                undeployedJars[index++] = jarClassLoader.getJarName();
                try {
                    undeployedJars[index++] = ClassPathLoader.getLatest().getJarDeployer().undeploy(jarClassLoader.getJarName());
                } catch (IllegalArgumentException iaex) {
                    // It's okay for it to have have been uneployed from this server
                    undeployedJars[index++] = iaex.getMessage();
                }
            }
        } else {
            List<String> undeployedList = new ArrayList<String>();
            StringTokenizer jarTokenizer = new StringTokenizer(jarFilenameList, ",");
            while (jarTokenizer.hasMoreTokens()) {
                String jarFilename = jarTokenizer.nextToken().trim();
                try {
                    undeployedList.add(jarFilename);
                    undeployedList.add(ClassPathLoader.getLatest().getJarDeployer().undeploy(jarFilename));
                } catch (IllegalArgumentException iaex) {
                    // It's okay for it to not have been deployed to this server
                    undeployedList.add(iaex.getMessage());
                }
            }
            undeployedJars = undeployedList.toArray(undeployedJars);
        }
        CliFunctionResult result = new CliFunctionResult(memberId, undeployedJars);
        context.getResultSender().lastResult(result);
    } catch (CacheClosedException cce) {
        CliFunctionResult result = new CliFunctionResult(memberId, false, null);
        context.getResultSender().lastResult(result);
    } catch (VirtualMachineError e) {
        SystemFailure.initiateFailure(e);
        throw e;
    } catch (Throwable th) {
        SystemFailure.checkFailure();
        logger.error("Could not undeploy JAR file: {}", th.getMessage(), th);
        CliFunctionResult result = new CliFunctionResult(memberId, th, null);
        context.getResultSender().lastResult(result);
    }
}
Also used : DeployedJar(org.apache.geode.internal.DeployedJar) ArrayList(java.util.ArrayList) InternalCache(org.apache.geode.internal.cache.InternalCache) CacheClosedException(org.apache.geode.cache.CacheClosedException) StringTokenizer(java.util.StringTokenizer) DistributedMember(org.apache.geode.distributed.DistributedMember) JarDeployer(org.apache.geode.internal.JarDeployer)

Example 5 with JarDeployer

use of org.apache.geode.internal.JarDeployer in project geode by apache.

the class ClusterConfigurationLoader method deployJarsReceivedFromClusterConfiguration.

/**
   * Deploys the jars received from shared configuration, it undeploys any other jars that were not
   * part of shared configuration
   * 
   * @param cache Cache of this member
   * @param response {@link ConfigurationResponse} received from the locators
   */
public static void deployJarsReceivedFromClusterConfiguration(Cache cache, ConfigurationResponse response) throws IOException, ClassNotFoundException {
    logger.info("Requesting cluster configuration");
    if (response == null) {
        return;
    }
    String[] jarFileNames = response.getJarNames();
    byte[][] jarBytes = response.getJars();
    logger.info("Got response with jars: {}", Stream.of(jarFileNames).collect(joining(",")));
    if (jarFileNames != null && jarBytes != null) {
        JarDeployer jarDeployer = ClassPathLoader.getLatest().getJarDeployer();
        jarDeployer.suspendAll();
        try {
            List<String> extraJarsOnServer = jarDeployer.findDeployedJars().stream().map(DeployedJar::getJarName).filter(jarName -> !ArrayUtils.contains(jarFileNames, jarName)).collect(toList());
            for (String extraJar : extraJarsOnServer) {
                logger.info("Removing jar not present in cluster configuration: {}", extraJar);
                jarDeployer.deleteAllVersionsOfJar(extraJar);
            }
            List<DeployedJar> deployedJars = jarDeployer.deploy(jarFileNames, jarBytes);
            deployedJars.stream().filter(Objects::nonNull).forEach((jar) -> logger.info("Deployed: {}", jar.getFile().getAbsolutePath()));
        } finally {
            jarDeployer.resumeAll();
        }
    }
}
Also used : ClassPathLoader(org.apache.geode.internal.ClassPathLoader) StringUtils(org.apache.commons.lang.StringUtils) Arrays(java.util.Arrays) ClusterConfigurationService(org.apache.geode.distributed.internal.ClusterConfigurationService) DeployedJar(org.apache.geode.internal.DeployedJar) JarDeployer(org.apache.geode.internal.JarDeployer) LocalizedStrings(org.apache.geode.internal.i18n.LocalizedStrings) DistributionLocatorId(org.apache.geode.internal.admin.remote.DistributionLocatorId) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Cache(org.apache.geode.cache.Cache) ConfigSource(org.apache.geode.internal.ConfigSource) ConfigurationRequest(org.apache.geode.management.internal.configuration.messages.ConfigurationRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) UnmodifiableException(org.apache.geode.UnmodifiableException) LogService(org.apache.geode.internal.logging.LogService) Map(java.util.Map) LinkedList(java.util.LinkedList) ClusterConfigurationNotAvailableException(org.apache.geode.internal.process.ClusterConfigurationNotAvailableException) Properties(java.util.Properties) TcpClient(org.apache.geode.distributed.internal.tcpserver.TcpClient) Set(java.util.Set) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Collectors.joining(java.util.stream.Collectors.joining) Objects(java.util.Objects) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) DistributionConfig(org.apache.geode.distributed.internal.DistributionConfig) Logger(org.apache.logging.log4j.Logger) Stream(java.util.stream.Stream) Configuration(org.apache.geode.management.internal.configuration.domain.Configuration) ConfigurationResponse(org.apache.geode.management.internal.configuration.messages.ConfigurationResponse) ArrayUtils(org.apache.commons.lang.ArrayUtils) InputStream(java.io.InputStream) DeployedJar(org.apache.geode.internal.DeployedJar) JarDeployer(org.apache.geode.internal.JarDeployer)

Aggregations

DeployedJar (org.apache.geode.internal.DeployedJar)5 JarDeployer (org.apache.geode.internal.JarDeployer)5 ArrayList (java.util.ArrayList)3 CacheClosedException (org.apache.geode.cache.CacheClosedException)3 DistributedMember (org.apache.geode.distributed.DistributedMember)3 InternalCache (org.apache.geode.internal.cache.InternalCache)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 Arrays (java.util.Arrays)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Properties (java.util.Properties)1 Set (java.util.Set)1 StringTokenizer (java.util.StringTokenizer)1