Example usage for javax.management Attribute Attribute

List of usage examples for javax.management Attribute Attribute

Introduction

In this page you can find the example usage for javax.management Attribute Attribute.

Prototype

public Attribute(String name, Object value) 

Source Link

Document

Constructs an Attribute object which associates the given attribute name with the given value.

Usage

From source file:org.apache.servicemix.jbi.deployer.impl.ComponentInstaller.java

public void configure(Properties props) throws Exception {
    if (props != null && props.size() > 0) {
        ObjectName on = getInstallerConfigurationMBean();
        if (on == null) {
            LOGGER.warn(/*from w w w . j a v  a  2  s .  co m*/
                    "Could not find installation configuration MBean. Installation properties will be ignored.");
        } else {
            MBeanServer mbs = deployer.getMbeanServer();
            for (Object o : props.keySet()) {
                String key = (String) o;
                String val = props.getProperty(key);
                try {
                    mbs.setAttribute(on, new Attribute(key, val));
                } catch (JMException e) {
                    throw new DeploymentException("Could not set installation property: (" + key + " = " + val,
                            e);
                }
            }
        }
    }
}

From source file:com.app.server.SARDeployer.java

/**
 * This method extracts the SAR archive and configures for the SAR and starts the services
 * @param file/* ww  w  .j  a  v  a 2 s .c o m*/
 * @param warDirectoryPath
 * @throws IOException
 */
public void extractSarDeploy(ClassLoader cL, Object... args) throws IOException {
    CopyOnWriteArrayList classPath = null;
    File file = null;
    String fileName = "";
    String fileWithPath = "";
    if (args[0] instanceof File) {
        classPath = new CopyOnWriteArrayList();
        file = (File) args[0];
        fileWithPath = file.getAbsolutePath();
        ZipFile zip = new ZipFile(file);
        ZipEntry ze = null;
        fileName = file.getName();
        fileName = fileName.substring(0, fileName.indexOf('.'));
        fileName += "sar";
        String fileDirectory;
        Enumeration<? extends ZipEntry> entries = zip.entries();
        int numBytes;
        while (entries.hasMoreElements()) {
            ze = entries.nextElement();
            // //log.info("Unzipping " + ze.getName());
            String filePath = serverConfig.getDeploydirectory() + "/" + fileName + "/" + ze.getName();
            if (!ze.isDirectory()) {
                fileDirectory = filePath.substring(0, filePath.lastIndexOf('/'));
            } else {
                fileDirectory = filePath;
            }
            // //log.info(fileDirectory);
            createDirectory(fileDirectory);
            if (!ze.isDirectory()) {
                FileOutputStream fout = new FileOutputStream(filePath);
                byte[] inputbyt = new byte[8192];
                InputStream istream = zip.getInputStream(ze);
                while ((numBytes = istream.read(inputbyt, 0, inputbyt.length)) >= 0) {
                    fout.write(inputbyt, 0, numBytes);
                }
                fout.close();
                istream.close();
                if (ze.getName().endsWith(".jar")) {
                    classPath.add(filePath);
                }
            }
        }
        zip.close();
    } else if (args[0] instanceof FileObject) {
        FileObject fileObj = (FileObject) args[0];
        fileName = fileObj.getName().getBaseName();
        try {
            fileWithPath = fileObj.getURL().toURI().toString();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        fileName = fileName.substring(0, fileName.indexOf('.'));
        fileName += "sar";
        classPath = unpack(fileObj, new File(serverConfig.getDeploydirectory() + "/" + fileName + "/"),
                (StandardFileSystemManager) args[1]);
    }
    URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    URL[] urls = loader.getURLs();
    WebClassLoader sarClassLoader;
    if (cL != null) {
        sarClassLoader = new WebClassLoader(urls, cL);
    } else {
        sarClassLoader = new WebClassLoader(urls);
    }
    for (int index = 0; index < classPath.size(); index++) {
        // log.info("file:"+classPath.get(index));
        sarClassLoader.addURL(new URL("file:" + classPath.get(index)));
    }
    sarClassLoader.addURL(new URL("file:" + serverConfig.getDeploydirectory() + "/" + fileName + "/"));
    //log.info(sarClassLoader.geturlS());
    sarsMap.put(fileWithPath, sarClassLoader);
    try {
        Sar sar = (Sar) sardigester.parse(new InputSource(new FileInputStream(
                serverConfig.getDeploydirectory() + "/" + fileName + "/META-INF/" + "mbean-service.xml")));
        CopyOnWriteArrayList mbeans = sar.getMbean();
        //log.info(mbeanServer);
        ObjectName objName, classLoaderObjectName = new ObjectName("com.app.server:classLoader=" + fileName);
        if (!mbeanServer.isRegistered(classLoaderObjectName)) {
            mbeanServer.registerMBean(sarClassLoader, classLoaderObjectName);
        } else {
            mbeanServer.unregisterMBean(classLoaderObjectName);
            mbeanServer.registerMBean(sarClassLoader, classLoaderObjectName);
            ;
        }
        for (int index = 0; index < mbeans.size(); index++) {
            Mbean mbean = (Mbean) mbeans.get(index);
            //log.info(mbean.getObjectname());
            //log.info(mbean.getCls());
            objName = new ObjectName(mbean.getObjectname());
            Class service = sarClassLoader.loadClass(mbean.getCls());
            if (mbeanServer.isRegistered(objName)) {
                //mbs.invoke(objName, "stopService", null, null);
                //mbs.invoke(objName, "destroy", null, null);
                mbeanServer.unregisterMBean(objName);
            }
            mbeanServer.createMBean(service.getName(), objName, classLoaderObjectName);
            //mbs.registerMBean(obj, objName);
            CopyOnWriteArrayList attrlist = mbean.getMbeanAttribute();
            if (attrlist != null) {
                for (int count = 0; count < attrlist.size(); count++) {
                    MBeanAttribute attr = (MBeanAttribute) attrlist.get(count);
                    Attribute mbeanattribute = new Attribute(attr.getName(), attr.getValue());
                    mbeanServer.setAttribute(objName, mbeanattribute);
                }
            }
            Attribute mbeanattribute = new Attribute("ObjectName", objName);
            mbeanServer.setAttribute(objName, mbeanattribute);
            if (((String) mbeanServer.getAttribute(objName, "Deployer")).equals("true")) {
                mbeanServer.invoke(objName, "init", new Object[] { deployerList },
                        new String[] { Vector.class.getName() });

            }
            mbeanServer.invoke(objName, "init", new Object[] { serviceList, serverConfig, mbeanServer },
                    new String[] { Vector.class.getName(), ServerConfig.class.getName(),
                            MBeanServer.class.getName() });
            mbeanServer.invoke(objName, "start", null, null);
            serviceListObjName.put(fileWithPath, objName);
        }
    } catch (Exception e) {
        log.error("Could not able to deploy sar archive " + fileWithPath, e);
        // TODO Auto-generated catch block
        //e.printStackTrace();
    }
}

From source file:org.jumpmind.symmetric.SymmetricWebServer.java

protected void registerHttpJmxAdaptor(int jmxPort) throws Exception {
    if (AppUtils.isSystemPropertySet(SystemConstants.SYSPROP_JMX_HTTP_CONSOLE_ENABLED, true) && jmxEnabled) {
        log.info("Starting JMX HTTP console on port {}", jmxPort);
        MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
        ObjectName name = getHttpJmxAdaptorName();
        mbeanServer.createMBean(HttpAdaptor.class.getName(), name);
        if (!AppUtils.isSystemPropertySet(SystemConstants.SYSPROP_JMX_HTTP_CONSOLE_LOCALHOST_ENABLED, true)) {
            mbeanServer.setAttribute(name, new Attribute("Host", "0.0.0.0"));
        } else if (StringUtils.isNotBlank(host)) {
            mbeanServer.setAttribute(name, new Attribute("Host", host));
        }/*from   ww w . j a v  a  2s.c o  m*/
        mbeanServer.setAttribute(name, new Attribute("Port", new Integer(jmxPort)));
        ObjectName processorName = getXslJmxAdaptorName();
        mbeanServer.createMBean(XSLTProcessor.class.getName(), processorName);
        mbeanServer.setAttribute(name, new Attribute("ProcessorName", processorName));
        mbeanServer.invoke(name, "start", null, null);
    }
}

From source file:org.springframework.jmx.access.MBeanClientInterceptor.java

@Nullable
private Object invokeAttribute(PropertyDescriptor pd, MethodInvocation invocation)
        throws JMException, IOException {

    Assert.state(this.serverToUse != null, "No MBeanServerConnection available");

    String attributeName = JmxUtils.getAttributeName(pd, this.useStrictCasing);
    MBeanAttributeInfo inf = this.allowedAttributes.get(attributeName);
    // If no attribute is returned, we know that it is not defined in the
    // management interface.
    if (inf == null) {
        throw new InvalidInvocationException(
                "Attribute '" + pd.getName() + "' is not exposed on the management interface");
    }/*w  w w.  ja va2  s . c o m*/

    if (invocation.getMethod().equals(pd.getReadMethod())) {
        if (inf.isReadable()) {
            return this.serverToUse.getAttribute(this.objectName, attributeName);
        } else {
            throw new InvalidInvocationException("Attribute '" + attributeName + "' is not readable");
        }
    } else if (invocation.getMethod().equals(pd.getWriteMethod())) {
        if (inf.isWritable()) {
            this.serverToUse.setAttribute(this.objectName,
                    new Attribute(attributeName, invocation.getArguments()[0]));
            return null;
        } else {
            throw new InvalidInvocationException("Attribute '" + attributeName + "' is not writable");
        }
    } else {
        throw new IllegalStateException(
                "Method [" + invocation.getMethod() + "] is neither a bean property getter nor a setter");
    }
}

From source file:com.cyberway.issue.crawler.deciderules.DecideRuleSequenceTest.java

public void testFilter() throws InvalidAttributeValueException, URIException, AttributeNotFoundException,
        MBeanException, ReflectionException {
    FilterDecideRule dr = new FilterDecideRule("FilterDecideRule(ContentTypeRegExpFilter)");
    addDecideRule(dr);//from  w  w  w . j av  a 2  s.co  m
    StringBuffer baseUri = new StringBuffer();
    UURI uuri = UURIFactory.getInstance("http://example.com/foo");
    CrawlURI curi = new CrawlURI(uuri);
    curi.setContentType("text/html");
    Object decision = this.rule.decisionFor(curi);
    // default for unconfigured FilterDecideRule is true from (empty)
    // filters, then ACCEPT because of true
    assertTrue("Expect " + DecideRule.ACCEPT + " but got " + decision, decision == DecideRule.ACCEPT);
    ContentTypeRegExpFilter filt = new ContentTypeRegExpFilter("ContentTypeRegExpFilter", "app.*");
    dr.filters.addElement(null, filt);
    decision = this.rule.decisionFor(curi);
    // filter should now return false, making decision REJECT
    assertTrue("Expect " + DecideRule.REJECT + " but got " + decision, decision == DecideRule.REJECT);
    curi.setContentType("application/octet-stream");
    decision = this.rule.decisionFor(curi);
    // filter should now return true, making decision ACCEPT
    assertTrue("Expect " + DecideRule.ACCEPT + " but got " + decision, decision == DecideRule.ACCEPT);
    // change true answer to "PASS"; use String to simulate settings non-identity
    dr.setAttribute(new Attribute(FilterDecideRule.ATTR_TRUE_DECISION, "PASS"));
    decision = this.rule.decisionFor(curi);
    assertTrue("Expect " + DecideRule.PASS + " but got " + decision, decision == DecideRule.PASS);
}

From source file:com.cyberway.issue.crawler.deciderules.DecideRuleSequenceTest.java

public void testContentTypeMatchesRegexpDecideRule() throws Exception {
    ContentTypeMatchesRegExpDecideRule dr = new ContentTypeMatchesRegExpDecideRule("CTMREDRtest");
    DecideRule v = addDecideRule(dr);//ww  w . j a  va 2  s .  co  m

    v.setAttribute(new Attribute(MatchesRegExpDecideRule.ATTR_REGEXP, "text/html"));
    UURI uuri = UURIFactory.getInstance("http://www.archive.org");
    CrawlURI crawlUri = new CrawlURI(uuri);

    // no content type - let curi pass
    Object decision = this.rule.decisionFor(crawlUri);
    assertTrue("URI Expect " + DecideRule.PASS + " for " + crawlUri + " but got " + decision,
            decision == DecideRule.PASS);

    // non-matching content type - let curi pass
    crawlUri.setContentType("application/pdf");
    decision = this.rule.decisionFor(crawlUri);
    assertTrue("URI Expect " + DecideRule.PASS + " for " + crawlUri + " but got " + decision,
            decision == DecideRule.PASS);

    // matching content type - accept curi
    crawlUri.setContentType("text/html");
    decision = this.rule.decisionFor(crawlUri);
    assertTrue("URI Expect " + DecideRule.ACCEPT + " for " + crawlUri + " but got " + decision,
            decision == DecideRule.ACCEPT);
}

From source file:com.cyberway.issue.crawler.deciderules.DecideRuleSequenceTest.java

public void testContentTypeNotMatchesRegexpDecideRule() throws Exception {
    ContentTypeNotMatchesRegExpDecideRule dr = new ContentTypeNotMatchesRegExpDecideRule("CTNMREDRtest");
    DecideRule v = addDecideRule(dr);/*w  ww  . ja  v  a 2s.  co m*/

    v.setAttribute(new Attribute(MatchesRegExpDecideRule.ATTR_REGEXP, "text/html"));
    UURI uuri = UURIFactory.getInstance("http://www.archive.org");
    CrawlURI crawlUri = new CrawlURI(uuri);

    // no content type - let curi pass
    Object decision = this.rule.decisionFor(crawlUri);
    assertTrue("URI Expect " + DecideRule.PASS + " for " + crawlUri + " but got " + decision,
            decision == DecideRule.PASS);

    // matching content type - let curi pass
    crawlUri.setContentType("text/html");
    decision = this.rule.decisionFor(crawlUri);
    assertTrue("URI Expect " + DecideRule.PASS + " for " + crawlUri + " but got " + decision,
            decision == DecideRule.PASS);

    // non-matching content type - accept curi
    crawlUri.setContentType("application/pdf");
    decision = this.rule.decisionFor(crawlUri);
    assertTrue("URI Expect " + DecideRule.ACCEPT + " for " + crawlUri + " but got " + decision,
            decision == DecideRule.ACCEPT);
}

From source file:com.tc.stats.DSO.java

@Override
public Map<ObjectName, Exception> setAttribute(Set<ObjectName> onSet, String attrName, Object attrValue) {
    Map<ObjectName, Exception> result = new HashMap<ObjectName, Exception>();
    Iterator<ObjectName> onIter = onSet.iterator();
    Attribute attribute = new Attribute(attrName, attrValue);
    ObjectName on;/*from   w  w  w .ja  va 2  s.  c o  m*/
    while (onIter.hasNext()) {
        on = onIter.next();
        try {
            mbeanServer.setAttribute(on, attribute);
        } catch (Exception e) {
            result.put(on, newPlainException(e));
        }
    }
    return result;
}

From source file:com.tc.stats.DSO.java

@Override
public Map<ObjectName, Exception> setAttribute(String attrName, Map<ObjectName, Object> attrMap) {
    Map<ObjectName, Exception> result = new HashMap<ObjectName, Exception>();
    Iterator<Entry<ObjectName, Object>> entryIter = attrMap.entrySet().iterator();
    while (entryIter.hasNext()) {
        Entry<ObjectName, Object> entry = entryIter.next();
        ObjectName on = entry.getKey();
        try {/*from ww w  .  j  ava2  s .  c  o m*/
            Attribute attribute = new Attribute(attrName, entry.getValue());
            mbeanServer.setAttribute(on, attribute);
        } catch (Exception e) {
            result.put(on, newPlainException(e));
        }
    }
    return result;
}

From source file:net.lightbody.bmp.proxy.jetty.util.jmx.ModelMBeanImpl.java

public AttributeList getAttributes(String[] names) {
    log.debug("getAttributes");
    AttributeList results = new AttributeList(names.length);
    for (int i = 0; i < names.length; i++) {
        try {/*from  w  w  w  .  ja va2  s . c  o m*/
            results.add(new Attribute(names[i], getAttribute(names[i])));
        } catch (Exception e) {
            log.warn(LogSupport.EXCEPTION, e);
        }
    }
    return results;
}