/*
 * Copyright 2008-2012 Red Hat, Inc, and individual contributors.
 * 
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.immutant.daemons;

import org.immutant.core.ClassLoaderUtils;
import org.immutant.daemons.as.DaemonServices;
import org.jboss.as.server.deployment.DeploymentUnit;
import org.jboss.msc.service.ServiceController;
import org.jboss.msc.service.ServiceName;
import org.projectodd.polyglot.core_extensions.AtRuntimeInstaller;


public class Daemonizer extends AtRuntimeInstaller<Daemonizer> {

    public Daemonizer(DeploymentUnit unit) {
        super( unit );
    }

    public Daemon createDaemon(final String daemonName, Runnable start, Runnable stop, boolean singleton) {

        ServiceName serviceName = DaemonServices.daemon( getUnit(), daemonName );
        ServiceController<?> service = getUnit().getServiceRegistry().getService( serviceName ); 
        Daemon daemon;
        if (service == null) {
            daemon = new Daemon(ClassLoaderUtils.getModuleLoader( getUnit() ), start, stop);
            deploy( serviceName, daemon, singleton );
            installMBean( serviceName, "immutant.daemons", daemon );
        } else {
            daemon = (Daemon)service.getValue();
            daemon.stop();
            daemon.setStartFunction( start );
            daemon.setStopFunction( stop );
            daemon.start();
        }
        
        return daemon;

    }
    
}

@@ -1025,56 +1025,8 @@
 it;%0A
-import org.jboss.msc.service.ServiceController;%0A
 impo
@@ -1381,17 +1381,16 @@
 eton) %7B%0A
-%0A
         
@@ -1475,163 +1475,14 @@
     
-ServiceController%3C?%3E service = getUnit().getServiceRegistry().getService( serviceName ); %0A        Daemon daemon;%0A        if (service == null) %7B%0A           
+Daemon
  dae
@@ -1498,16 +1498,17 @@
  Daemon(
+ 
 ClassLoa
@@ -1557,16 +1557,17 @@
 rt, stop
+ 
 );%0A     
@@ -1565,24 +1565,29 @@
  );%0A        
+%0A    
     deploy( 
@@ -1628,20 +1628,16 @@
         
-    
 installM
@@ -1689,229 +1689,8 @@
  );%0A
-        %7D else %7B%0A            daemon = (Daemon)service.getValue();%0A            daemon.stop();%0A            daemon.setStartFunction( start );%0A            daemon.setStopFunction( stop );%0A            daemon.start();%0A        %7D%0A
     
@@ -1717,17 +1717,16 @@
 daemon;%0A
-%0A
     %7D%0A  

Switch the daemonizer back to what it was, since IMMUTANT-140 is now solved by polyglot.