added a bundle com.sap.sse.threadmanager which provides a servlet for listing, suspending and resuming threads

This commit is contained in:
Axel Uhl
2015-03-19 08:40:04 +01:00
parent 3657caad04
commit a0500581f6
11 changed files with 338 additions and 0 deletions
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="resources"/>
<classpathentry kind="output" path="bin"/>
</classpath>
+28
View File
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>com.sap.sse.threadmanager</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
@@ -0,0 +1,7 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
@@ -0,0 +1,21 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Threadmanager
Bundle-SymbolicName: com.sap.sse.threadmanager
Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: SAP
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Require-Bundle: com.sun.jersey;bundle-version="1.17.0",
javax.servlet;bundle-version="3.1.0",
org.apache.shiro.core;bundle-version="1.2.2",
org.apache.shiro.web;bundle-version="1.2.2",
org.json.simple;bundle-version="1.1.0",
com.sap.sse.security,
com.sap.sse.security.common,
com.sap.sse.security.userstore.mongodb,
com.sap.sse,
com.sap.sse.common
Import-Package: javax.ws.rs;version="1.1.1",
javax.ws.rs.core,
org.osgi.framework;version="1.8.0"
Web-ContextPath: /threadmanager
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!-- Disable directory listing: -->
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.eclipse.jetty.servlet.DefaultServlet
</servlet-class>
<init-param>
<param-name>dirAllowed</param-name>
<param-value>false</param-value>
</init-param>
</servlet>
<!-- Apache Shiro -->
<context-param>
<param-name>shiroEnvironmentClass</param-name>
<param-value>org.apache.shiro.web.env.IniWebEnvironment</param-value>
</context-param>
<listener>
<listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
</listener>
<filter>
<filter-name>ShiroFilter</filter-name>
<filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
</filter>
<!-- Make sure any request you want accessible to Shiro is filtered. "/*"
catches all requests. Usually this filter mapping is defined first (before all
others) to ensure that Shiro works in subsequent filters in the filter chain: -->
<filter-mapping>
<filter-name>ShiroFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>Jersey REST API</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.sap.sse.threadmanager.RestApiApplication</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>com.sap.sse.threadmanager.AccessControlAndEncodingResponseFilter</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST API</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
@@ -0,0 +1,6 @@
source.. = src/,\
resources/
output.. = bin/
bin.includes = META-INF/,\
.,\
WEB-INF/
+12
View File
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>root</artifactId>
<groupId>com.sap.sailing</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>com.sap.sse.threadmanager</artifactId>
<packaging>eclipse-plugin</packaging>
</project>
@@ -0,0 +1,66 @@
[main]
credentialsMatcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
credentialsMatcher.storedCredentialsHexEncoded = false
credentialsMatcher.hashIterations = 1024
# Realm configuration:
# --------------------
# Uncomment the following line to specify a bundle-specific permissions-for-role provider
# permissionsForRoleProvider = com.sap.myapp....MyAppPermissionsForRoleProvider
upRealm = com.sap.sse.security.UsernamePasswordRealm
upRealm.credentialsMatcher = $credentialsMatcher
# Uncomment the following line make the upRealm aware of the permissionsForRoleProvider
# upRealm.permissionsForRoleProvider = $permissionsForRoleProvider
oauthRealm = com.sap.sse.security.OAuthRealm
# Uncomment the following line make the oauthRealm aware of the permissionsForRoleProvider
# oauthRealm.permissionsForRoleProvider = $permissionsForRoleProvider
bearerTokenRealm = com.sap.sse.security.BearerTokenRealm
# Uncomment the following line make the bearerTokenRealm aware of the permissionsForRoleProvider
# bearerTokenRealm.permissionsForRoleProvider = $permissionsForRoleProvider
# Putting it all together:
sessionManager = com.sap.sse.security.SecurityWebSessionManager
securityManager.sessionManager = $sessionManager
sessionDAO = org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO
securityManager.sessionManager.sessionDAO = $sessionDAO
cacheManager = com.sap.sse.security.SessionCacheManager
securityManager.cacheManager = $cacheManager
# Authentication Filter Configurations
# ------------------------------------
# A filter that requires the user to have at least one of the roles specified as
# filter parameter. This makes it different from the default roles[...] filter
# which requires the user to have ALL of the roles specified as parameters.
anyofroles = com.sap.sse.security.AnyOfRolesFilter
# A custom filter for GWT pages that require an authenticated user.
# Forwards unauthenticated users to the default login page. When
# addressed directly (and not based on a redirect), users who logged
# on successfully will be redirected to the UserManagement.html page
# where they can adjust their profile.
customGwt = com.sap.sse.security.CustomFilter
customGwt.loginUrl = /security/ui/Login.html
customGwt.successUrl = /UserManagement.html
# Configuration for the default shiro HTTP form authentication filter.
# It assumes that form-based login parameters are sent to the
# /api/restsecurity/login RESTlet and forwards successful login attempts
# to the /api/restsecurity/hello service which is expected to respond
# with a JSON document containing the authenticated subject's properties.
authc.loginUrl = /api/restsecurity/login
authc.successUrl = /api/restsecurity/hello
# This authentication filter accepts a bearer access token in the HTTP
# Authorization header field, as in
# Authorization: Bearer 1029741026501365024376093245
# If no such bearer token is provided, the filter falls back to basic HTTP
# authentication ("Authentication: Basic ...") and then regular form-based
# authentication with POST parameters "username" and "password".
bearerToken = com.sap.sse.security.BearerTokenOrBasicOrFormAuthenticationFilter
# Specifying filter chains for URL patterns
[urls]
@@ -0,0 +1,20 @@
package com.sap.sse.threadmanager;
import javax.ws.rs.core.MultivaluedMap;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;
public class AccessControlAndEncodingResponseFilter implements ContainerResponseFilter {
@Override
public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {
MultivaluedMap<String, Object> httpHeaders = response.getHttpHeaders();
httpHeaders.add("Access-Control-Allow-Origin", "*");
httpHeaders.add("Access-Control-Allow-Headers", "Authorization, Origin, X-Requested-With, Content-Type");
httpHeaders.add("Access-Control-Expose-Headers", "Location, Content-Disposition");
httpHeaders.add("Access-Control-Allow-Methods", "POST, PUT, GET, DELETE, HEAD, OPTIONS");
return response;
}
}
@@ -0,0 +1,14 @@
package com.sap.sse.threadmanager;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
public class RestApiApplication extends Application {
public Set<Class<?>> getClasses() {
HashSet<Class<?>> classes = new HashSet<>();
classes.add(ThreadManager.class);
return classes;
}
}
@@ -0,0 +1,95 @@
package com.sap.sse.threadmanager;
import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@Path("/threads")
public class ThreadManager {
@Context ServletContext servletContext;
@Path("")
@GET
@Produces("application/json;charset=UTF-8")
public Response getThreads() {
JSONArray threadsJson = new JSONArray();
Thread[] threads = new Thread[10000];
Thread.enumerate(threads);
for (Thread t : threads) {
if (t != null) {
JSONObject threadJson = new JSONObject();
threadJson.put("name", t.getName());
threadJson.put("id", t.getId());
threadJson.put("daemon", t.isDaemon());
threadJson.put("alive", t.isAlive());
threadJson.put("threadgroup", t.getThreadGroup().getName());
threadsJson.add(threadJson);
}
}
String json = threadsJson.toJSONString();
return Response.ok(json, MediaType.APPLICATION_JSON).build();
}
@SuppressWarnings("deprecation") // using Thread.suspend()
@Path("{name}/suspend")
@GET
@Produces("application/json;charset=UTF-8")
public Response suspend(@PathParam("name") String name) {
final Response response;
JSONObject result = new JSONObject();
Thread[] threads = new Thread[10000];
boolean found = false;
Thread.enumerate(threads);
for (Thread t : threads) {
if (t != null && t.getName().equals(name)) {
t.suspend();
result.put("status", "OK");
found = true;
}
}
String json = result.toJSONString();
if (!found) {
result.put("status", "Not found");
response = Response.status(Status.NOT_FOUND).entity(json).build();
} else {
response = Response.ok(json, MediaType.APPLICATION_JSON).build();
}
return response;
}
@SuppressWarnings("deprecation") // using Thread.suspend()
@Path("{name}/resume")
@GET
@Produces("application/json;charset=UTF-8")
public Response resume(@PathParam("name") String name) {
final Response response;
JSONObject result = new JSONObject();
Thread[] threads = new Thread[10000];
boolean found = false;
Thread.enumerate(threads);
for (Thread t : threads) {
if (t != null && t.getName().equals(name)) {
t.resume();
result.put("status", "OK");
found = true;
}
}
String json = result.toJSONString();
if (!found) {
result.put("status", "Not found");
response = Response.status(Status.NOT_FOUND).entity(json).build();
} else {
response = Response.ok(json, MediaType.APPLICATION_JSON).build();
}
return response;
}
}