Author: hiranya
Date: Thu Apr  5 08:54:08 2012
New Revision: 1309702

URL: http://svn.apache.org/viewvc?rev=1309702&view=rev
Log:
Adding basic SNMPv1 and SNMPv2c support

Added:
    
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/
    
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SNMPAgent.java
    
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SNMPConstants.java
    
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SynapseMIBUtils.java
    
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SynapseMOScalar.java
    
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SynapseSNMPAgent.java
Modified:
    synapse/trunk/java/modules/commons/pom.xml
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/Axis2SynapseController.java
    synapse/trunk/java/modules/distribution/src/main/assembly/bin.xml
    synapse/trunk/java/pom.xml

Modified: synapse/trunk/java/modules/commons/pom.xml
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/commons/pom.xml?rev=1309702&r1=1309701&r2=1309702&view=diff
==============================================================================
--- synapse/trunk/java/modules/commons/pom.xml (original)
+++ synapse/trunk/java/modules/commons/pom.xml Thu Apr  5 08:54:08 2012
@@ -95,5 +95,13 @@
             <groupId>xmlunit</groupId>
             <artifactId>xmlunit</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.snmp4j</groupId>
+            <artifactId>snmp4j-agent</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.snmp4j</groupId>
+            <artifactId>snmp4j</artifactId>
+        </dependency>
     </dependencies>
 </project>

Added: 
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SNMPAgent.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SNMPAgent.java?rev=1309702&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SNMPAgent.java
 (added)
+++ 
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SNMPAgent.java
 Thu Apr  5 08:54:08 2012
@@ -0,0 +1,268 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.apache.synapse.commons.snmp;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.snmp4j.TransportMapping;
+import org.snmp4j.agent.BaseAgent;
+import org.snmp4j.agent.CommandProcessor;
+import org.snmp4j.agent.DuplicateRegistrationException;
+import org.snmp4j.agent.ManagedObject;
+import org.snmp4j.agent.io.ImportModes;
+import org.snmp4j.agent.mo.MOTableRow;
+import org.snmp4j.agent.mo.snmp.*;
+import org.snmp4j.agent.security.MutableVACM;
+import org.snmp4j.mp.MPv3;
+import org.snmp4j.mp.SnmpConstants;
+import org.snmp4j.security.*;
+import org.snmp4j.smi.*;
+import org.snmp4j.transport.TransportMappings;
+
+import javax.management.*;
+import java.io.File;
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.util.*;
+
+/**
+ * SNMP agent which is capable of listening for incoming SNMP GET/GETNEXT 
requests
+ * and responding to them accordingly. This agent implementation exposed all 
the
+ * standard Synapse MBeans over SNMP. The view exposed by the agent is 
read-only as of
+ * now (this may be changed in a future version). The relevant OID mappings are
+ * defined in the SynapseMIBUtils class. Each MBean attribute becomes a leaf 
in the MIB
+ * exposed by the agent. For each MBean, attributes are arranged in the 
alphabetical
+ * order for OID assignment. MBean APIs rarely change. Therefore this scheme 
will
+ * guarantee a fairly consistent OID scheme.
+ */
+class SNMPAgent extends BaseAgent {
+
+    private static final Log log = LogFactory.getLog(SNMPAgent.class);
+    
+    private static final String FULL_READ_VIEW = "fullReadView";
+    private static final String GROUP_NAME = "synapseSNMPGroup";
+    private static final String COMMUNITY_RECORD = "public2public";
+
+    private Properties properties;
+
+    private Set<OID> registeredOIDs = new HashSet<OID>();
+    private int snmpVersion;
+
+    public SNMPAgent(Properties properties) {
+        super(new File(SNMPConstants.BC_FILE), new 
File(SNMPConstants.CONFIG_FILE),
+                new CommandProcessor(new 
OctetString(MPv3.createLocalEngineID())));
+        this.properties = properties;
+        
+        String version = getProperty(SNMPConstants.SNMP_VERSION, 
SNMPConstants.SNMP_DEFAULT_VERSION);
+        if (SNMPConstants.SNMP_VERSION_1.equals(version)) {
+            this.snmpVersion = SnmpConstants.version1;
+        } else if (SNMPConstants.SNMP_VERSION_2_C.equals(version)) {
+            this.snmpVersion = SnmpConstants.version2c;
+        } else {
+            log.warn("Unsupported SNMP version: " + version + " - Using 
defaults");
+            this.snmpVersion = SnmpConstants.version1;
+        }
+    }
+
+    /**
+     * Initialize and start this SNMP agent
+     *
+     * @throws IOException If an error occurs while initializing the agent
+     */
+    public void start() throws IOException {
+        String context = getProperty(SNMPConstants.SNMP_CONTEXT_NAME,
+                SNMPConstants.SNMP_DEFAULT_CONTEXT_NAME);
+        init();
+        loadConfig(ImportModes.REPLACE_CREATE);
+        addShutdownHook();
+        getServer().addContext(new OctetString(context));
+        finishInit();
+        run();
+        sendColdStartNotification();
+    }
+
+    @Override
+    protected void initTransportMappings() throws IOException {
+        String host = getProperty(SNMPConstants.SNMP_HOST, 
SNMPConstants.SNMP_DEFAULT_HOST);
+        int port = Integer.parseInt(getProperty(SNMPConstants.SNMP_PORT,
+                String.valueOf(SNMPConstants.SNMP_DEFAULT_PORT)));
+        String address = host + "/" + port;
+        Address adr = GenericAddress.parse(address);
+        TransportMapping tm =
+                TransportMappings.getInstance().createTransportMapping(adr);
+        transportMappings = new TransportMapping[] { tm };
+        log.info("SNMP transport adapter initialized on udp:" + address);
+    }
+
+    @Override
+    protected void registerManagedObjects() {
+        log.info("Initializing Synapse SNMP MIB");
+        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+        Set<ObjectInstance> instances = mbs.queryMBeans(null, null);
+
+        try {
+            for (ObjectInstance instance : instances) {
+                ObjectName objectName = instance.getObjectName();
+                if (objectName.getDomain().equals("org.apache.synapse")) {
+                    String oidString = SynapseMIBUtils.getOID(objectName);
+                    if (oidString == null) {
+                        continue;
+                    }
+                    
+                    MBeanInfo info = mbs.getMBeanInfo(objectName);
+                    MBeanAttributeInfo[] attributes = info.getAttributes();
+                    List<String> attributeNames = new ArrayList<String>();
+                    List<String> mapAttributes = new ArrayList<String>();
+                    for (MBeanAttributeInfo attributeInfo : attributes) {
+                        attributeNames.add(attributeInfo.getName());
+                        if 
(Map.class.getName().equals(attributeInfo.getType())) {
+                            mapAttributes.add(attributeInfo.getName());
+                        }
+                    }
+                    Collections.sort(attributeNames);
+
+                    doRegister(attributeNames, mapAttributes, oidString, 
objectName);
+                }
+            }
+        } catch (Exception e) {
+            log.error("Error while initializing the SNMP MIB", e);
+        }
+    }
+    
+    private void doRegister(List<String> attributeNames, List<String> 
mapAttributes, 
+                            String oidString, ObjectName objectName) {
+        
+        for (int i = 0; i < attributeNames.size(); i++) {
+            String attributeName = attributeNames.get(i);
+            if (mapAttributes.contains(attributeName)) {
+                continue;
+            }
+            OID oid = new OID(oidString + "." + (i + 1) + ".0");
+            if (log.isDebugEnabled()) {
+                log.debug("Registering " + objectName + "@" + attributeName +
+                        " as OID: " + oid);
+            }
+            try {
+                server.register(new SynapseMOScalar(
+                        oid, objectName, attributeName, snmpVersion), null);
+                registeredOIDs.add(oid);
+            } catch (DuplicateRegistrationException e) {
+                log.error("Error while registering the OID: " + oid + " for 
object: " +
+                        objectName + " and attribute: " + attributeName, e);
+            }
+        }    
+    }
+
+    @Override
+    protected void unregisterManagedObjects() {
+        if (log.isDebugEnabled()) {
+            log.debug("Cleaning up registered OIDs");
+        }
+
+        for (OID oid : registeredOIDs) {
+            ManagedObject mo = server.getManagedObject(oid, null);
+            if (mo != null) {
+                server.unregister(mo, null);
+            }
+        }
+        registeredOIDs.clear();
+    }
+
+    @Override
+    protected void addUsmUser(USM usm) {
+
+    }
+
+    @Override
+    protected void addNotificationTargets(SnmpTargetMIB snmpTargetMIB,
+                                          SnmpNotificationMIB 
snmpNotificationMIB) {
+        
+    }
+
+    @Override
+    protected void addViews(VacmMIB vacm) {
+        String communityString = 
getProperty(SNMPConstants.SNMP_COMMUNITY_NAME, 
+                SNMPConstants.SNMP_DEFAULT_COMMUNITY_NAME);
+        String securityName = getProperty(SNMPConstants.SNMP_SECURITY_NAME,
+                SNMPConstants.SNMP_DEFAULT_SECURITY_NAME);
+
+        int securityModel = SecurityModel.SECURITY_MODEL_SNMPv1;
+        if (snmpVersion == SnmpConstants.version2c) {
+            securityModel = SecurityModel.SECURITY_MODEL_SNMPv2c;
+        }
+
+        vacm.addGroup(securityModel,
+                new OctetString(securityName),
+                new OctetString(GROUP_NAME),
+                StorageType.nonVolatile);
+
+        vacm.addAccess(new OctetString(GROUP_NAME), new 
OctetString(communityString),
+                securityModel,
+                SecurityLevel.NOAUTH_NOPRIV,
+                MutableVACM.VACM_MATCH_EXACT,
+                new OctetString(FULL_READ_VIEW), // read permission granted
+                new OctetString(),               // no write permissions
+                new OctetString(),               // no notify permissions
+                StorageType.nonVolatile);
+
+        vacm.addViewTreeFamily(new OctetString(FULL_READ_VIEW),
+                new OID(SNMPConstants.SYNAPSE_OID_BRANCH),
+                new OctetString(),
+                VacmMIB.vacmViewIncluded,
+                StorageType.nonVolatile);
+    }
+
+    @Override
+    protected void addCommunities(SnmpCommunityMIB communityMIB) {
+        String community = getProperty(SNMPConstants.SNMP_COMMUNITY_NAME, 
+                SNMPConstants.SNMP_DEFAULT_COMMUNITY_NAME);
+        String securityName = getProperty(SNMPConstants.SNMP_SECURITY_NAME,
+                SNMPConstants.SNMP_DEFAULT_SECURITY_NAME);
+        String context = getProperty(SNMPConstants.SNMP_CONTEXT_NAME,
+                SNMPConstants.SNMP_DEFAULT_CONTEXT_NAME);
+
+        if (log.isDebugEnabled()) {
+            log.debug("Registering SNMP community string: " + community + " 
under the " +
+                    "context: " + context);
+        }
+
+        Variable[] com2sec = new Variable[] {
+                new OctetString(community),              // community name
+                new OctetString(securityName),           // security name
+                getAgent().getContextEngineID(),         // local engine ID
+                new OctetString(context),                // default context 
name
+                new OctetString(),                       // transport tag
+                new Integer32(StorageType.nonVolatile),  // storage type
+                new Integer32(RowStatus.active)          // row status
+        };
+        MOTableRow row =
+                communityMIB.getSnmpCommunityEntry().createRow(
+                        new OctetString(COMMUNITY_RECORD).toSubIndex(true), 
com2sec);
+        communityMIB.getSnmpCommunityEntry().addRow(row);
+    }
+    
+    private String getProperty(String name, String def) {
+        String value = properties.getProperty(name);
+        if (value == null) {
+            value = def;
+        }
+        return value;
+    }
+}

Added: 
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SNMPConstants.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SNMPConstants.java?rev=1309702&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SNMPConstants.java
 (added)
+++ 
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SNMPConstants.java
 Thu Apr  5 08:54:08 2012
@@ -0,0 +1,55 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.apache.synapse.commons.snmp;
+
+public class SNMPConstants {
+
+    /**
+     * This OID branch has been uniquely assigned to the Synapse project by 
the ASF.
+     * Please do not change.
+     * 
+     * @see <a 
href="https://cwiki.apache.org/confluence/display/DIRxPMGT/OID+Assignment+Scheme";>ASF
 OID Assignments</a>
+     */
+    public static final String SYNAPSE_OID_BRANCH = "1.3.6.1.4.1.18060.14";
+    
+    public static final String SNMP_VERSION_1 = "snmpv1";
+    public static final String SNMP_VERSION_2_C = "snmpv2c";
+    
+    // Configuration parameters
+    public static final String SNMP_ENABLED = "synapse.snmp.enabled";
+    public static final String SNMP_COMMUNITY_NAME = 
"synapse.snmp.community.name";
+    public static final String SNMP_SECURITY_NAME = 
"synapse.snmp.security.name";
+    public static final String SNMP_CONTEXT_NAME  = 
"synapse.snmp.context.name";
+    public static final String SNMP_HOST = "synapse.snmp.host";
+    public static final String SNMP_PORT = "synapse.snmp.port";
+    public static final String SNMP_VERSION = "synapse.snmp.version";
+
+    // Configuration defaults
+    public static final String SNMP_DEFAULT_COMMUNITY_NAME = "public";
+    public static final String SNMP_DEFAULT_SECURITY_NAME = "cpublic";
+    public static final String SNMP_DEFAULT_CONTEXT_NAME = "public";
+    public static final String SNMP_DEFAULT_HOST = "127.0.0.1";
+    public static final int SNMP_DEFAULT_PORT = 9161;
+    public static final String SNMP_DEFAULT_VERSION = SNMP_VERSION_1;
+
+    public static final String BC_FILE = "./logs/snmp/boot-counter.cfg";
+    public static final String CONFIG_FILE = "./logs/snmp/conf.cfg";
+
+}

Added: 
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SynapseMIBUtils.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SynapseMIBUtils.java?rev=1309702&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SynapseMIBUtils.java
 (added)
+++ 
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SynapseMIBUtils.java
 Thu Apr  5 08:54:08 2012
@@ -0,0 +1,101 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.apache.synapse.commons.snmp;
+
+import javax.management.ObjectName;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Utilities for generating Synapse OIDs and MIB entries
+ */
+public class SynapseMIBUtils {
+
+    private static final String PROPERTY_CONNECTOR_NAME = "ConnectorName";
+    private static final String PROPERTY_NAME = "Name";
+    private static final String PROPERTY_TYPE = "Type";
+
+    /**
+     * Contains MBean name to OID mappings. MBeans which are not included
+     * in this map are not exposed over SNMP.
+     */
+    private static final Map<String,Integer> type2oid = new HashMap<String, 
Integer>();
+
+    /**
+     * A basic symbol table to ensure consistent OID assignment for MBean
+     * attributes.
+     */
+    private static final Map<String,Integer> name2oid = new HashMap<String, 
Integer>();
+
+    static {
+        type2oid.put("ServerManager", 1);
+        type2oid.put("Transport", 2);
+        type2oid.put("NhttpConnections", 3);
+        type2oid.put("NHTTPLatencyView", 4);
+        type2oid.put("NHTTPS2SLatencyView", 5);
+
+        name2oid.put("nio-http-listener", 1);
+        name2oid.put("nio-http-sender", 2);
+        name2oid.put("nio-https-listener", 3);
+        name2oid.put("nio-https-sender", 4);
+        name2oid.put("jms-listener", 5);
+        name2oid.put("jms-sender", 6);
+        name2oid.put("vfs-listener", 7);
+        name2oid.put("vfs-sender", 8);
+        name2oid.put("mailto-listener", 9);
+        name2oid.put("mailto-sender", 10);
+        name2oid.put("http-listener", 11);
+        name2oid.put("http-sender", 12);
+        name2oid.put("https-listener", 13);
+        name2oid.put("https-sender", 14);
+        name2oid.put("nio-http", 15);
+        name2oid.put("nio-https", 16);
+    }
+
+    public synchronized static String getOID(ObjectName objectName) {
+        String type = objectName.getKeyProperty(PROPERTY_TYPE);
+        Integer typeOID = type2oid.get(type);
+        if (typeOID == null) {
+            return null;
+        }
+
+        String name;
+        if ("Transport".equals(type)) {
+            // ditch the time stamp suffix at the end of the connector name
+            String connector = 
objectName.getKeyProperty(PROPERTY_CONNECTOR_NAME);
+            name = connector.substring(0, connector.lastIndexOf('-'));
+        } else {
+            name = objectName.getKeyProperty(PROPERTY_NAME);
+        }
+        
+        if (name != null) {
+            Integer nameOID = name2oid.get(name);
+            if (nameOID == null) {
+                nameOID = new Integer(name2oid.size() + 1);
+                name2oid.put(name, nameOID);
+            }
+            return SNMPConstants.SYNAPSE_OID_BRANCH + "." + typeOID.intValue() 
+
+                    "." + nameOID.intValue();
+        } else {
+            return SNMPConstants.SYNAPSE_OID_BRANCH + "." + typeOID.intValue();
+        }
+    }
+
+}

Added: 
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SynapseMOScalar.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SynapseMOScalar.java?rev=1309702&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SynapseMOScalar.java
 (added)
+++ 
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SynapseMOScalar.java
 Thu Apr  5 08:54:08 2012
@@ -0,0 +1,75 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.apache.synapse.commons.snmp;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.snmp4j.agent.mo.MOAccessImpl;
+import org.snmp4j.agent.mo.MOScalar;
+import org.snmp4j.mp.SnmpConstants;
+import org.snmp4j.smi.*;
+
+import javax.management.*;
+import java.lang.management.ManagementFactory;
+import java.util.Date;
+
+/**
+ * Synapse managed object scalar implementation. This class queries built-in 
JMX MBeans
+ * to retrieve the value of the specified OID. 
+ */
+public class SynapseMOScalar extends MOScalar<Variable> {
+    
+    private static final Log log = LogFactory.getLog(SynapseMOScalar.class);
+    
+    private ObjectName objectName;
+    private String attribute;
+    private int snmpVersion;
+
+    public SynapseMOScalar(OID id, ObjectName objectName, String attribute, 
int snmpVersion) {
+        super(id, MOAccessImpl.ACCESS_READ_ONLY, new OctetString());
+        this.objectName = objectName;
+        this.attribute = attribute;
+        this.snmpVersion = snmpVersion;
+    }
+
+    @Override
+    public Variable getValue() {
+        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+        try {
+            Object obj = mbs.getAttribute(objectName, attribute);
+            if (obj instanceof Integer) {
+                return new Integer32((Integer) obj);
+            }
+            
+            if (snmpVersion > SnmpConstants.version1) {
+                if (obj instanceof Long) {
+                    return new Counter64(((Long) obj).longValue());
+                } else if (obj instanceof Date) {
+                    return new Counter64(((Date) obj).getTime());
+                }
+            }
+
+            return new OctetString(obj.toString());
+        } catch (Exception e) {
+            log.error("Unexpected error while retrieving the value of OID: " + 
getID(), e);
+            return null;
+        }
+    }
+}

Added: 
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SynapseSNMPAgent.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SynapseSNMPAgent.java?rev=1309702&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SynapseSNMPAgent.java
 (added)
+++ 
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/snmp/SynapseSNMPAgent.java
 Thu Apr  5 08:54:08 2012
@@ -0,0 +1,61 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.apache.synapse.commons.snmp;
+
+import java.io.IOException;
+import java.util.Properties;
+
+/**
+ * This is the public API for initializing and stopping the Synapse SNMP agent.
+ */
+public class SynapseSNMPAgent {
+
+    /*
+     * The purpose of this class is to conceal the API of the SNMPAgent class 
which
+     * is an extension of the SNMP4J BaseAgent class. The BaseAgent exposes a 
whole
+     * bunch of methods which can be used to alter the behavior of the SNMP 
agent.
+     * We certainly don't want other modules messing around with these 
methods. So
+     * we have given package access to the SNMPAgent class and wrapped it up 
using
+     * the SynapseSNMPAgent which has a much cleaner and simple API.
+     */
+
+    private SNMPAgent agent;
+    
+    public SynapseSNMPAgent(Properties properties) {
+        this.agent = new SNMPAgent(properties);
+    }
+
+    /**
+     * Start the SNMP agent for Synapse. This will initialize the SNMP 
transport bindings,
+     * initialize the MIB and start the SNMP agent to accept incoming requests.
+     *
+     * @throws IOException If an error occurs while starting the SNMP agent
+     */
+    public void start() throws IOException {
+        agent.start();
+    }
+
+    /**
+     * Stop and shutdown the Synapse SNMP agent.
+     */
+    public void stop() {
+        agent.stop();
+    }
+}

Modified: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/Axis2SynapseController.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/Axis2SynapseController.java?rev=1309702&r1=1309701&r2=1309702&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/Axis2SynapseController.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/Axis2SynapseController.java
 Thu Apr  5 08:54:08 2012
@@ -31,11 +31,14 @@ import org.apache.axis2.format.BinaryBui
 import org.apache.axis2.format.PlainTextBuilder;
 import org.apache.axis2.phaseresolver.PhaseException;
 import org.apache.axis2.phaseresolver.PhaseMetadata;
+import org.apache.axis2.util.JavaUtils;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.synapse.commons.datasource.DataSourceRepositoryHolder;
+import org.apache.synapse.commons.snmp.SNMPConstants;
+import org.apache.synapse.commons.snmp.SynapseSNMPAgent;
 import org.apache.synapse.commons.util.RMIRegistryController;
 import org.apache.synapse.config.*;
 import org.apache.synapse.libraries.imports.SynapseImport;
@@ -58,6 +61,7 @@ import org.apache.synapse.util.xpath.ext
 import org.apache.synapse.util.xpath.ext.XpathExtensionUtil;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.*;
 
 /**
@@ -95,6 +99,8 @@ public class Axis2SynapseController impl
     /** JMX Adapter */
     private JmxAdapter jmxAdapter;
 
+    private SynapseSNMPAgent snmpAgent;
+
     private TaskDescriptionRepository taskDescriptionRepository;
 
     private TaskScheduler taskScheduler;
@@ -265,6 +271,9 @@ public class Axis2SynapseController impl
 
                 stopJmxAdapter();
                 RMIRegistryController.getInstance().shutDown();
+                if (snmpAgent != null) {
+                    snmpAgent.stop();
+                }
 
                 // we need to call this method to clean the temp files we 
created.
                 if (configurationContext != null) {
@@ -324,6 +333,17 @@ public class Axis2SynapseController impl
                         + jmxAdapter.getJmxInformation().getJmxUrl());
             }
         }
+
+        Properties properties = 
SynapsePropertiesLoader.loadSynapseProperties();
+        String enabled = properties.getProperty(SNMPConstants.SNMP_ENABLED);
+        try {
+            if (enabled != null && JavaUtils.isTrueExplicitly(enabled)) {
+                snmpAgent = new SynapseSNMPAgent(properties);
+                snmpAgent.start();
+            }
+        } catch (IOException e) {
+            log.error("Error while initializing SNMP", e);
+        }
     }
 
     /**

Modified: synapse/trunk/java/modules/distribution/src/main/assembly/bin.xml
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/distribution/src/main/assembly/bin.xml?rev=1309702&r1=1309701&r2=1309702&view=diff
==============================================================================
--- synapse/trunk/java/modules/distribution/src/main/assembly/bin.xml (original)
+++ synapse/trunk/java/modules/distribution/src/main/assembly/bin.xml Thu Apr  
5 08:54:08 2012
@@ -188,8 +188,10 @@
                 <exclude>xerces:xercesImpl:jar</exclude>
                 <exclude>org.apache.xerces:xercesImpl</exclude> <!-- Find the 
source of this -->
                 <exclude>org.apache.xerces:xml-apis</exclude>
-               <exclude>xalan:xalan:jar</exclude>
+                   <exclude>xalan:xalan:jar</exclude>
                 <exclude>quickfixj:quickfixj-all:jar</exclude>
+                <exclude>org.snmp4j:snmp4j</exclude>
+                <exclude>org.snmp4j:snmp4j-agent</exclude>
             </excludes>
         </dependencySet>
        <dependencySet>

Modified: synapse/trunk/java/pom.xml
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/pom.xml?rev=1309702&r1=1309701&r2=1309702&view=diff
==============================================================================
--- synapse/trunk/java/pom.xml (original)
+++ synapse/trunk/java/pom.xml Thu Apr  5 08:54:08 2012
@@ -986,6 +986,16 @@
                 <artifactId>xercesImpl</artifactId>
                 <version>${xerces.version}</version>
             </dependency>
+            <dependency>
+                <groupId>org.snmp4j</groupId>
+                <artifactId>snmp4j-agent</artifactId>
+                <version>${snmp4j.agent.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.snmp4j</groupId>
+                <artifactId>snmp4j</artifactId>
+                <version>${snmp4j.version}</version>
+            </dependency>
         </dependencies>
     </dependencyManagement>
 
@@ -1043,6 +1053,17 @@
                 <updatePolicy>interval:10080</updatePolicy>
             </snapshots>
         </repository>
+        <repository>
+            <id>snmp4j-repo</id>
+            <name>SNMP4J Repository</name>
+            <url>https://server.oosnmp.net/dist/release/</url>
+            <releases>
+                <updatePolicy>never</updatePolicy>
+            </releases>
+            <snapshots>
+                <enabled>false</enabled>
+            </snapshots>
+        </repository>
     </repositories>
 
     <modules>
@@ -1123,6 +1144,8 @@
         <wso2uri-template.version>1.0.0</wso2uri-template.version>
         <woden.version>1.0M9</woden.version>
         <activemq.version>5.2.0</activemq.version>
+        <snmp4j.version>2.0.3</snmp4j.version>
+        <snmp4j.agent.version>2.0.5</snmp4j.agent.version>
 
         <!-- dependencies of Synapse extensions module -->
         <wso2commons.version>1.2</wso2commons.version>


Reply via email to