⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ruleservice.java

📁 一个开源的rfid middleware 资料
💻 JAVA
字号:
/*
 * Copyright 2005 i-Konect LLC
 * 
 * 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.firstopen.singularity.cep;

import java.rmi.RemoteException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.ejb.CreateException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.Notification;
import javax.management.NotificationListener;
import javax.management.ReflectionException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.drools.FactException;
import org.drools.WorkingMemory;
import org.firstopen.singularity.ale.AleSLSBHome;
import org.firstopen.singularity.ale.AleSLSBRemote;
import org.firstopen.singularity.system.ReaderEvent;
import org.firstopen.singularity.util.DAOUtilFactory;
import org.jboss.system.ServiceMBeanSupport;

/**
 * @author TomRose
 * 
 */
public class RuleService extends ServiceMBeanSupport implements
        RuleServiceMBean, NotificationListener {

    private Log log = LogFactory.getLog(this.getClass());

    private RuleBaseList ruleBaseList = new RuleBaseList();

    public void createService() throws Exception {
      
        addNotifyListener(this);
    }

    public void startService() throws Exception {
        loadRuleBase();
    }

    public void stopService() throws Exception {

    }

    public void destoryService() throws Exception {

    }

    /**
     * Get Rule sets from persistance storeage. DB contains the XML for the
     * rules.
     * 
     */
    public void loadRuleBase() throws RulesNotFoundException {

        
        
        RuleBaseDAO ruleBaseDAO = RuleBaseDAOFactory.create();

        ruleBaseList.setRulebases(ruleBaseDAO.getAllRuleBases());

        ruleChangeNotification("rules have changed, cached reloaded");
        
        DAOUtilFactory.close();
    }

    /**
     * 
     * @param listener
     * @throws InstanceNotFoundException
     */
    protected void addNotifyListener(NotificationListener listener)
            throws InstanceNotFoundException {
        getServer().addNotificationListener(getServiceName(), listener, null,
                null);
    }

    /**
     * Called by the ALE service
     * 
     * @param ecspec
     * @param events
     * @throws FactException
     */
    public void assertEvents(String ecspec, List<ReaderEvent> events)
            throws FactException {

        List ruleBases = ruleBaseList.getRuleBases(ecspec);
        for (Iterator iter = ruleBases.iterator(); iter.hasNext();) {
            StatefulRuleBase rulebase = (StatefulRuleBase) iter.next();
            WorkingMemory workingMemory = rulebase.getWorkingMemory();

            for (Iterator iterator = events.iterator(); iterator.hasNext();) {
                ReaderEvent event = (ReaderEvent) iterator.next();
                workingMemory.assertObject(event);

            }// end for all events
            workingMemory.fireAllRules();

        }// end for each ecspec
    }

    /**
     * 
     * @param rulebaseName
     */
    public void resetSessionState(String rulebaseName) {
        ruleBaseList.getRuleBase(rulebaseName).newWorkingMemory();
    }

    /**
     * 
     * <p>
     * Broadcasts a notification to the cluster partition. * * This example does
     * not ensure that a notification sequence number * is unique throughout the
     * partition.
     * </p>
     * 
     * @param message
     * @throws InstanceNotFoundException
     * @throws MBeanException
     * @throws ReflectionException
     */
    public void ruleChangeNotification(String message) {

        long now = System.currentTimeMillis();

        Notification notification = new Notification(
                "org.firstopen.singularity.rulechange", super.getServiceName(),
                now, now, message);

        sendNotification(notification);
    }

    /**
     * 
     * <p>
     * call back for notification listener
     * </p>
     * 
     * @param notification
     * @param handback
     */
    public void handleNotification(Notification notification, Object handback) {
        log.debug("notification recveived");

    }

    /**
     * 
     * @param rulebasename
     */
    public void printECSpec(String rulebasename) {
        StatefulRuleBase rulebase = ruleBaseList.getRuleBase(rulebasename);

        Set<String> ecspecList = rulebase.getEcspecs();
        for (Iterator iter = ecspecList.iterator(); iter.hasNext();) {
            String element = (String) iter.next();
            log.debug("element = " + element);

        }
    }

    /**
     * @param rulebasename
     * @param ecspec
     */
    public void addECSpec(String rulebasename, String ecspec) {

        ruleBaseList.addECSpec(rulebasename, ecspec);

    }

    /**
     * Subscribe with a local JMX service uri
     *
     */
    protected void subscribeALE() {
        try {
            Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY,
                    "org.jnp.interfaces.NamingContextFactory");
            env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
            env.put(Context.URL_PKG_PREFIXES,
                    "org.jboss.naming:org.jnp.interfaces");
            InitialContext jndiContext = new InitialContext(env);
            AleSLSBHome aHome = (AleSLSBHome) jndiContext
                    .lookup("ejb/ale/AleSLSB");
            AleSLSBRemote aSLSB = aHome.create();
            
            Set<String> ecSpecList = ruleBaseList.getAllECSpecs();
            for (Iterator iter = ecSpecList.iterator(); iter.hasNext();) {
                String element = (String) iter.next();
                aSLSB.subscribe(element,"jmx://localhost/"+serviceName);
            }//end for all ecSpecs
        } catch (NamingException e) {
            log.error(e);
        } catch (RemoteException e) {
            log.error(e);
        } catch (CreateException e) {

            log.error(e);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}// end RuleService

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -