Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 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.camel.web.connectors;

import java.util.List;

/**
*
*/
public interface CamelConnection {

public CamelDataBean getCamelBean(String type, String name);

public List<CamelDataBean> getCamelBeans(String type);

public Object invokeOperation(String type, String name, String operationName, Object[] params, String[] signature);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* 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.camel.web.connectors;

import java.io.IOException;

import org.apache.camel.web.connectors.jmx.CamelJmxConnection;

/**
*
*/
public class CamelConnectionFactory {

private static final String JMX_SERVICE_URL_PREFIX = "service:jmx:rmi:///jndi/rmi://";

private String jmxHost = "localhost";

private String jmxPort = "1099";

private String jmxPath = "jmxrmi/camel";

private static CamelConnectionFactory instance = new CamelConnectionFactory();

private CamelConnectionFactory() {}

public static CamelConnectionFactory getInstance() {
return instance;
}

public CamelConnection getJmxConnection() throws IOException {
String jmxServiceUrl = JMX_SERVICE_URL_PREFIX + jmxHost + ":" + jmxPort + "/" + jmxPath;
return new CamelJmxConnection(jmxServiceUrl);
}

public CamelConnection getJmxConnection(String jmxHost, String jmxPort, String jmxPath) throws IOException {
String jmxServiceUrl = JMX_SERVICE_URL_PREFIX + jmxHost + ":" + jmxPort + "/" + jmxPath;
return new CamelJmxConnection(jmxServiceUrl);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* 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.camel.web.connectors;

import java.util.HashMap;
import java.util.Map;

/**
* Camel Managed Bean representation
*/
public class CamelDataBean {

private final Map<String, Object> properties = new HashMap<String, Object>();

private String name;

private String description;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Object getProperty(String propertyName) {
return properties.get(propertyName);
}

public Map<String, Object> getProperties() {
return properties;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* 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.camel.web.connectors.jmx;

import javax.management.*;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.apache.camel.web.connectors.CamelConnection;
import org.apache.camel.web.connectors.CamelDataBean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
*
*/
public class CamelJmxConnection implements CamelConnection {

private final MBeanServerConnection connection;

public CamelJmxConnection(String jmxServiceUrl) throws IOException {
JMXServiceURL url = new JMXServiceURL(jmxServiceUrl);
//TODO Handle credentials
JMXConnector connector = JMXConnectorFactory.connect(url);
connection = connector.getMBeanServerConnection();
}

protected MBeanInfo getMBeanInfo(ObjectInstance objInstance) throws IntrospectionException, InstanceNotFoundException, IOException, ReflectionException {
//TODO Caching MBeanInfo
MBeanInfo mBeanInfo = connection.getMBeanInfo(objInstance.getObjectName());
return mBeanInfo;
}

protected Set<ObjectInstance> getObjectInstances(String type) throws IOException, MalformedObjectNameException {
Set<ObjectInstance> beans = connection.queryMBeans(new ObjectName("org.apache.camel:type=" + type + ",*"), null);
return beans;
}

ObjectInstance getObjectInstance(String type, String name) throws MalformedObjectNameException,
NullPointerException, IOException {
Set<ObjectInstance> beans = connection.queryMBeans(new ObjectName("org.apache.camel:type=" + type + ",name="
+ name + ",*"), null);
return beans.isEmpty() ? null : beans.iterator().next();
}

public CamelDataBean getCamelBean(String type, String name) {
CamelDataBean bean;
try {
ObjectInstance instance = getObjectInstance(type, name);
MBeanInfo info = getMBeanInfo(instance);
bean = new CamelBeanFactory().build(instance, connection, info);
} catch (Exception e) {
throw new RuntimeException(e);
}

return bean;
}

public List<CamelDataBean> getCamelBeans(String type) {
List<CamelDataBean> endpoints = new ArrayList<CamelDataBean>();
try {
Set<ObjectInstance> beans = getObjectInstances(type);
for (ObjectInstance instance : beans) {
MBeanInfo info = getMBeanInfo(instance);
CamelDataBean endpoint = new CamelBeanFactory().build(instance, connection, info);
endpoints.add(endpoint);
}
} catch (Exception e) {
throw new RuntimeException(e);
}

return endpoints;
}

public Object invokeOperation(String type, String name, String operationName, Object[] params, String[] signature) {
Object result;
try {
ObjectInstance instance = getObjectInstance(type, name);
result = connection.invoke(instance.getObjectName(), operationName, params, signature);
} catch (Exception e) {
throw new RuntimeException(e);
}

return result;
}

private class CamelBeanFactory {

public CamelDataBean build(ObjectInstance instance, MBeanServerConnection connection, MBeanInfo info)
throws InstanceNotFoundException, IOException, ReflectionException {

CamelDataBean c = new CamelDataBean();
String name = instance.getObjectName().getKeyProperty("name");
if (name.endsWith("\""))
name = name.substring(0, name.length() - 1);
if (name.startsWith("\""))
name = name.substring(1);

c.setName(name);
c.setDescription(info.getDescription());

MBeanAttributeInfo[] attributes = info.getAttributes();
List<String> attributeNames = new ArrayList<String>();
for (MBeanAttributeInfo attribute : attributes) {
attributeNames.add(attribute.getName());
}

List attributeList = connection.getAttributes(instance.getObjectName(), attributeNames.toArray(new String[0]));
List<Attribute> list = attributeList;
for (Attribute attribute : list) {
c.getProperties().put(attribute.getName(), attribute.getValue());
}

return c;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* 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.camel.web.model;

import org.apache.camel.web.connectors.CamelDataBean;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

/**
* Consumer
*/
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Consumer {

public static final String PROPERTY_STATUS = "State";
public static final String PROPERTY_ENDPOINT_URI = "EndpointUri";
public static final String PROPERTY_ROUTE_ID = "RouteId";

@XmlAttribute
private String name;

private String description;

private String status;

private String endpointUri;

private String routeId;

public void load(CamelDataBean bean) {
name = bean.getName();
description = bean.getDescription();
status = (String) bean.getProperty(PROPERTY_STATUS);
endpointUri = (String) bean.getProperty(PROPERTY_ENDPOINT_URI);
routeId = (String) bean.getProperty(PROPERTY_ROUTE_ID);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getEndpointUri() {
return endpointUri;
}

public void setEndpointUri(String endpointUri) {
this.endpointUri = endpointUri;
}

public String getRouteId() {
return routeId;
}

public void setRouteId(String routeId) {
this.routeId = routeId;
}

public boolean isStartable() {
if(!status.equals("Started"))
return true;
return false;
}

public boolean isStoppable() {
if(!status.equals("Stopped"))
return true;
return false;
}
}
Loading