?? basicrouter.java
字號:
/* * Copyright (c) 2005, John Mettraux, OpenWFE.org * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * . Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * . Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * . Neither the name of the "OpenWFE" nor the names of its contributors may be * used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * $Id: BasicRouter.java,v 1.8 2005/05/17 16:40:18 jmettraux Exp $ *///// BasicRouter.java//// jmettraux@openwfe.org//// generated with // jtmpl 1.0.04 31.10.2002 John Mettraux (jmettraux@openwfe.org)//package openwfe.org.apre.impl;import openwfe.org.Utils;import openwfe.org.Parameters;import openwfe.org.ServiceException;import openwfe.org.ApplicationContext;import openwfe.org.Parameters;import openwfe.org.xml.XmlUtils;import openwfe.org.apre.Agent;import openwfe.org.apre.Router;import openwfe.org.apre.AgentBuilder;import openwfe.org.apre.ApreException;import openwfe.org.engine.Definitions;/** * A basic router, that fetches its info from an xml config file. * * <p><font size=2>CVS Info : * <br>$Author: jmettraux $ * <br>$Date: 2005/05/17 16:40:18 $ * <br>$Id: BasicRouter.java,v 1.8 2005/05/17 16:40:18 jmettraux Exp $ </font> * * @author jmettraux@openwfe.org */public class BasicRouter extends Router{ private final static org.apache.log4j.Logger log = org.apache.log4j.Logger .getLogger(BasicRouter.class.getName()); // // INNER CLASSES abstract class Entry { java.util.List matches = null; public Entry (final java.util.List matches) { this.matches = matches; /* log.debug("Entry() matches.size = "+this.matches.size()); java.util.Iterator it = this.matches.iterator(); while (it.hasNext()) log.debug("Entry() match >"+it.next()+"<"); */ } public boolean matches (final String participantName) { log.debug("matches() participantName >"+participantName+"<"); java.util.Iterator it = matches.iterator(); while (it.hasNext()) { String pattern = (String)it.next(); if (participantName.matches(pattern)) return true; } return false; } public java.util.List getMatchList () { return this.matches; } public abstract Agent getAgent () throws ApreException; } class InstantiateEachTimeEntry extends Entry { String agentBuilder = null; String agentName = null; String agentClass = null; java.util.Map params = null; public InstantiateEachTimeEntry (final String agentBuilder, final String agentName, final String agentClass, final java.util.Map params, final java.util.List matches) { super(matches); this.agentBuilder = agentBuilder; this.agentName = agentName; this.agentClass = agentClass; this.params = params; } public Agent getAgent () throws ApreException { log.debug("instantiating agent (each time)"); return instantiateAgent (this.agentBuilder, this.agentName, this.agentClass, this.params); } } class InstantiateOnceEntry extends Entry { Agent agent = null; public InstantiateOnceEntry (final Agent agent, final java.util.List matches) { super(matches); this.agent = agent; } public Agent getAgent () throws ApreException { log.debug("returning instantiated agent (once)"); return this.agent; } } // // CONSTANTS & co public final static String NAME = "name"; public final static String CLASS = "class"; public final static String AGENT = "agent"; public final static String MATCHES = "matches"; public final static String BUILDER = "builder"; public final static String AGENT_FILE = "agentFile"; public final static String INSTANTIATE = "instantiate"; public final static String INSTANTIATE_DEFAULT_VALUE = "once"; // // FIELDS protected java.util.List entries = null; // // CONSTRUCTORS public void init (final String serviceName, final ApplicationContext context, final java.util.Map serviceParams) throws ServiceException { super.init(serviceName, context, serviceParams); initAgents((String)getParams().get(AGENT_FILE)); log.debug("Service '"+getName()+"' ready for work."); // // display OpenWFE version in logs log.info("OpenWFE version : "+Definitions.OPENWFE_VERSION); } // // METHODS protected void initAgents (String agentFileName) throws ServiceException { if (agentFileName == null) { throw new ServiceException ("Parameter '"+AGENT_FILE+ "' is mandatory for service '"+getName()+"'"); } agentFileName = Utils.expandUrl (getContext().getApplicationDirectory(), agentFileName); org.jdom.Element rootElt = null; try { rootElt = XmlUtils.extractXml(agentFileName, false); } catch (Exception e) { throw new ServiceException ("Failed to build agents from "+agentFileName, e); } this.entries = new java.util.ArrayList(); java.util.Iterator it = rootElt.getChildren(AGENT).iterator(); while (it.hasNext()) { org.jdom.Element elt = (org.jdom.Element)it.next(); String agentName = elt.getAttributeValue(NAME); String agentBuilder = elt.getAttributeValue(BUILDER); String agentClass = elt.getAttributeValue(CLASS); String instantiate = elt.getAttributeValue(INSTANTIATE); if (instantiate == null) instantiate = INSTANTIATE_DEFAULT_VALUE; else instantiate = instantiate.trim().toLowerCase(); java.util.Map params = Parameters.extractParameters(elt); java.util.List matches = extractMatchList(elt); try { Entry entry = null; if (instantiate.equals("once")) { Agent agent = instantiateAgent (agentBuilder, agentName, agentClass, params); entry = new InstantiateOnceEntry(agent, matches); } else { entry = new InstantiateEachTimeEntry (agentBuilder, agentName, agentClass, params, matches); } this.entries.add(entry); log.debug ("Prepared agent "+ agentName+"::"+agentBuilder+"::"+agentClass); } catch (ApreException re) { log.debug("Failed to instantiate agent. Skipping it", re); } } } protected java.util.List extractMatchList (org.jdom.Element elt) { java.util.List result = new java.util.ArrayList(); java.util.Iterator it = elt.getChildren(MATCHES).iterator(); while (it.hasNext()) { org.jdom.Element eMatch = (org.jdom.Element)it.next(); result.add(eMatch.getTextTrim()); } return result; } protected Agent instantiateAgent (final String agentBuilder, final String agentName, final String agentClass, final java.util.Map params) throws ApreException { try { Class agentBuilderClass = Class.forName(agentBuilder); AgentBuilder builder = (AgentBuilder)agentBuilderClass.newInstance(); Agent agent = builder .build(agentName, agentClass, getContext(), params); return agent; } catch (ApreException re) { throw re; } catch (Exception e) { throw new ApreException ("Failed to instantiate agent "+ agentBuilder+"::"+agentClass, e); } } // // METHODS from Router public Agent determineAgent (final String participantName) throws ApreException { java.util.Iterator it = this.entries.iterator(); while (it.hasNext()) { Entry entry = (Entry)it.next(); if (entry.matches(participantName)) return entry.getAgent(); } return null; } // // METHODS from Service public org.jdom.Element getStatus () { final org.jdom.Element result = new org.jdom.Element(getName()); result.addContent(XmlUtils.getClassElt(this)); result.addContent(XmlUtils.getRevisionElt("$Id: BasicRouter.java,v 1.8 2005/05/17 16:40:18 jmettraux Exp $")); /* org.jdom.Element entriesElt = new org.jdom.Element("entries"); java.util.Iterator it = this.entries.iterator(); while (it.hasNext()) { Entry entry = (Entry)it.next(); org.jdom.Element entryElt = new org.jdom.Element("entry"); org.jdom.Element agentElt = new org.jdom.Element("agent"); agentElt.setAttribute ("class", entry.getAgent().getClass().getName()); entryElt.addContent(agentElt); entriesElt.addContent(entryElt); } result.addContent(entriesElt); */ return result; }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -