/*
 * Copyright to the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.rioproject.cybernode;

import net.jini.config.ConfigurationException;
import org.rioproject.config.AggregateConfig;
import org.rioproject.config.GroovyConfig;
import org.rioproject.opstring.ServiceBeanConfig;
import org.rioproject.core.jsb.ServiceBeanContext;
import org.rioproject.jsb.JSBContext;
import org.rioproject.log.LoggerConfig;

import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Map;

/**
 * Service termination helper.
 */
public class ServiceTerminationHelper {

    /**
     * Cleanup resources that may cause memory leaks
     *
     * @param context The <code>ServiceBeanContext</code> to use
     */
    public static void cleanup(ServiceBeanContext context) {
        if (context != null) {
            /* Release any created Handlers */
            Map map = context.getServiceBeanConfig().getConfigurationParameters();
            LoggerConfig[] loggerConfigs = (LoggerConfig[]) map.get(ServiceBeanConfig.LOGGER);
            if (loggerConfigs != null) {
                for (LoggerConfig loggerConfig : loggerConfigs) {
                    loggerConfig.close();
                }
            }

            /* If we have a groovy configuration, clean the configuration, clearing
             * the class cache and setting the embedded groovy classloader to null */
            try {
                if (context.getConfiguration() instanceof AggregateConfig) {
                    AggregateConfig ac = (AggregateConfig) context.getConfiguration();
                    if (ac.getOuterConfiguration() instanceof GroovyConfig) {
                        ((GroovyConfig) ac.getOuterConfiguration()).clear();
                    }
                    if (context instanceof JSBContext)
                        ((JSBContext) context).setConfiguration(null);
                }
            } catch (ConfigurationException e) {
                /* */
            }
        }

        /* Services using groovy leak memory on undeployment. The object retaining a reference to the
         * GroovyClassLoader (that has as it's parent the ServiceClassLoader) is a static
         * org.codehaus.groovy.control.CompilationUnit field in the
         * org.codehaus.groovy.transform.ASTTransformationVisitor class */
        try {
            final Class clazz = loadClass("org.codehaus.groovy.transform.ASTTransformationVisitor");
            final Field compUnit = clazz.getDeclaredField("compUnit");
            compUnit.setAccessible(true);
            // static field
            compUnit.set(null, null);
        } catch (Throwable t) {
            // ignore
        }
    }

    static Class loadClass(final String className) {
        return AccessController.doPrivileged(new PrivilegedAction<Class>() {
            public Class run() {
                try {
                    return Thread.currentThread().getContextClassLoader().loadClass(className);
                } catch (ClassNotFoundException e) {
                    return null;
                }
            }
        });
    }

}

@@ -782,58 +782,8 @@
 ig;%0A
-import org.rioproject.opstring.ServiceBeanConfig;%0A
 impo
@@ -821,32 +821,32 @@
 iceBeanContext;%0A
+
 import org.riopr
@@ -1021,30 +1021,8 @@
 ion;
-%0Aimport java.util.Map;
 %0A%0A/*
@@ -1383,91 +1383,8 @@
  */%0A
-            Map map = context.getServiceBeanConfig().getConfigurationParameters();%0A
     
@@ -1426,33 +1426,19 @@
 s = 
-(LoggerConfig%5B%5D) map
+context
 .get
-(
 Serv
@@ -1454,15 +1454,28 @@
 nfig
-.LOGGER
+().getLoggerConfigs(
 );%0A 

Change to use loggerConfig property in ServiceBeanConfig