Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first spike of #125 to add a discovery mechanism to jolokia; which defau... #126

Closed
wants to merge 4 commits into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 75 additions & 0 deletions agent/core/src/main/java/org/jolokia/discovery/AgentDetails.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2009-2013 Roland Huss
*
* 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.jolokia.discovery;

/**
* a DTO used for discovery
*/
public class AgentDetails {
private String location;
private String name;

public AgentDetails(String location) {
this(location, null);
}

/**
* The location cannot be null; if no name is specified then a default value is used
*/
public AgentDetails(String location, String name) {
if (name == null) {
name = "Joloika Agent";
}
this.location = location;
this.name = name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

AgentDetails that = (AgentDetails) o;

if (!location.equals(that.location)) return false;
if (!name.equals(that.name)) return false;

return true;
}

@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + location.hashCode();
return result;
}

@Override
public String toString() {
return "AgentDetails{" +
"name='" + name + '\'' +
", location='" + location + '\'' +
'}';
}

public String getName() {
return name;
}

public String getLocation() {
return location;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2009-2013 Roland Huss
*
* 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.jolokia.discovery;

/**
* Listener interface for services which wish to register an interest in agents coming and going.
*/
public interface DiscoveryListener {
/**
* Invoked when an agent is started with the given details
*/
void onAgentStarted(AgentDetails details);

/**
* Invoked when an agent is stopped
*/
void onAgentStopped(AgentDetails details);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2009-2013 Roland Huss
*
* 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.jolokia.discovery;

import java.util.List;

/**
* Provides the common API to all discovery mbeans so they work consistently across JMX
*/
public interface DiscoveryMXBean {

/**
* Uses the underlying discovery mechanism (e.g. files on the file system or ZeroConf etc) to discover
* all the Jolokia Agents that can be found
*
* @return the found details (name and location URL) of the found jolokia agents
*/
List<AgentDetails> findAgents();
}
159 changes: 159 additions & 0 deletions agent/core/src/main/java/org/jolokia/discovery/JolokiaDiscovery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* Copyright 2009-2013 Roland Huss
*
* 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.jolokia.discovery;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* A little helper class so that we can broadcast the details of jolokia agents so its easier to
* auto-discover them on a machine, such as via files or zeroconf etc
*/
public class JolokiaDiscovery {
protected static JolokiaDiscovery instance = new JolokiaDiscovery();
private List<DiscoveryListener> listeners = new CopyOnWriteArrayList<DiscoveryListener>();
private AtomicBoolean loaded = new AtomicBoolean(false);

/**
* Returns the single instance
*/
public static JolokiaDiscovery getInstance() {
return instance;
}

/**
* Invoked when an agent is started on a given URL
*/
public void agentStarted(AgentDetails details) {
onStartupFindClassPathDiscoveryAgents();
for (DiscoveryListener listener : listeners) {
listener.onAgentStarted(details);
}
}

/**
* Invoked when an agent is stopped on a given URL
*/
public void agentStopped(AgentDetails details) {
onStartupFindClassPathDiscoveryAgents();
for (DiscoveryListener listener : listeners) {
listener.onAgentStopped(details);
}
}

public void addListener(DiscoveryListener listener) {
listeners.add(listener);
}

public void removeListener(DiscoveryListener listener) {
listeners.remove(listener);
}

/**
* Lets attempt to dynamically load a discovery agent if one is available on the classpath
*/
protected void onStartupFindClassPathDiscoveryAgents() {
if (loaded.compareAndSet(false, true)) {
Set<String> classNames = new HashSet<String>();

loadDiscoveryAgentClassNames(classNames, Thread.currentThread().getContextClassLoader());
loadDiscoveryAgentClassNames(classNames, getClass().getClassLoader());


for (String className : classNames) {
Class clazz = loadClass(className);
if (clazz != null) {
Object instance = null;
try {
instance = clazz.newInstance();
} catch (Exception e) {
System.out.println("Failed to instantiate class: " + clazz + ". " + e);
e.printStackTrace();
}
if (instance instanceof DiscoveryListener) {
addListener((DiscoveryListener) instance);
}
}
}
}
}

protected void loadDiscoveryAgentClassNames(Set<String> classNames, ClassLoader classLoader) {
String path = "META-INF/services/org/jolokia/discovery/DiscoveryListener";
try {
Enumeration<URL> iter = classLoader.getResources(path);
while (iter.hasMoreElements()) {
URL url = iter.nextElement();
if (url != null) {
InputStream in = url.openStream();
if (in != null) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while (true) {
String line = reader.readLine();
if (line != null) {
line = line.trim();
if (!line.startsWith("#") || line.length() > 0) {
classNames.add(line);
}
} else {
break;
}
}
} finally {
try {
in.close();
} catch (Exception e) {
// ignore
}
}

}
}
}
} catch (IOException e) {
System.out.println("Failed to discover services at " + path + ". " + e);
}
}

protected Class loadClass(String className) {
if (className != null && className.length() > 0) {
try {
return Class.forName(className);
} catch (Throwable e) {
try {
return Thread.currentThread().getContextClassLoader().loadClass(className);
} catch (Throwable e1) {
try {
return getClass().getClassLoader().loadClass(className);
} catch (Throwable e2) {
System.out.println("Failed to load class '" + className + "' on the classpath");
}
}
}
}
return null;
}
}