Author: rmannibucau
Date: Wed Jan 16 10:04:41 2013
New Revision: 1433877
URL: http://svn.apache.org/viewvc?rev=1433877&view=rev
Log:
more about log of jaxrs endpoints - make it still more user friendly
Added:
tomee/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/Logs.java
Modified:
tomee/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
Modified:
tomee/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
URL:
http://svn.apache.org/viewvc/tomee/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java?rev=1433877&r1=1433876&r2=1433877&view=diff
==============================================================================
---
tomee/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
(original)
+++
tomee/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
Wed Jan 16 10:04:41 2013
@@ -63,12 +63,10 @@ import javax.xml.bind.Marshaller;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.lang.reflect.ParameterizedType;
-import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -336,6 +334,10 @@ public class CxfRsHttpListener implement
base = prefix;
}
+ final List<Logs.LogResourceEndpointInfo> resourcesToLog = new
ArrayList<Logs.LogResourceEndpointInfo>();
+ int classSize = 0;
+ int addressSize = 0;
+
final JAXRSServiceImpl service = (JAXRSServiceImpl)
factory.getServiceFactory().getService();
final List<ClassResourceInfo> resources =
service.getClassResourceInfos();
for (final ClassResourceInfo info : resources) {
@@ -343,111 +345,60 @@ public class CxfRsHttpListener implement
continue;
}
- final String address = singleSlash(base,
info.getURITemplate().getValue());
+ final String address = Logs.singleSlash(base,
info.getURITemplate().getValue());
String clazz = info.getResourceClass().getName();
+ final String type;
if (restEjbs.containsKey(clazz)) {
- LOGGER.info(" Service URI: " + address + " -> EJB " +
clazz);
+ type = "EJB";
} else {
- LOGGER.info(" Service URI: " + address + " -> Pojo " +
clazz);
+ type = "Pojo";
}
- final MethodDispatcher md = info.getMethodDispatcher();
- for (OperationResourceInfo ori : md.getOperationResourceInfos()) {
- LOGGER.info(" "
- + forceLength(ori.getHttpMethod(), 7) + " " +
singleSlash(address, ori.getURITemplate().getValue())
- + " -> " + toGenericString(ori.getMethodToInvoke()));
- }
- }
- }
+ classSize = Math.max(classSize, clazz.length());
+ addressSize = Math.max(addressSize, address.length());
- private static String forceLength(final String httpMethod, final int l) {
- final String http;
- if (httpMethod == null) { // subresourcelocator implies null http
method
- http = "";
- } else {
- http = httpMethod;
- }
+ int methodSize = 7;
+ int methodStrSize = 0;
- final StringBuilder builder = new StringBuilder();
- for (int i = 0; i < l - http.length(); i++) {
- builder.append(" ");
- }
- return builder.append(http).toString();
- }
+ final List<Logs.LogOperationEndpointInfo> toLog = new
ArrayList<Logs.LogOperationEndpointInfo>();
- public static String toGenericString(final Method mtd) {
- try {
- final StringBuilder sb = new StringBuilder();
- final Type[] typeparms = mtd.getTypeParameters();
- if (typeparms.length > 0) {
- boolean first = true;
- sb.append("<");
- for(Type typeparm: typeparms) {
- if (!first) {
- sb.append(",");
- }
- sb.append(name(typeparm));
- first = false;
- }
- sb.append("> ");
- }
+ final MethodDispatcher md = info.getMethodDispatcher();
+ for (OperationResourceInfo ori : md.getOperationResourceInfos()) {
+ final String httpMethod = ori.getHttpMethod();
+ final String currentAddress = Logs.singleSlash(address,
ori.getURITemplate().getValue());
+ final String methodToStr =
Logs.toSimpleString(ori.getMethodToInvoke());
+ toLog.add(new Logs.LogOperationEndpointInfo(httpMethod,
currentAddress, methodToStr));
- final Type genRetType = mtd.getGenericReturnType();
- sb.append(name(genRetType)).append(" ");
- sb.append(mtd.getName()).append("(");
- final Type[] params = mtd.getGenericParameterTypes();
- for (int j = 0; j < params.length; j++) {
- sb.append(name(params[j]));
- if (j < (params.length - 1)) {
- sb.append(", ");
- }
- }
- sb.append(")");
- final Type[] exceptions = mtd.getGenericExceptionTypes();
- if (exceptions.length > 0) {
- sb.append(" throws ");
- for (int k = 0; k < exceptions.length; k++) {
- sb.append(name(exceptions[k]));
- if (k < (exceptions.length - 1)) {
- sb.append(",");
- }
+ if (httpMethod != null) {
+ methodSize = Math.max(methodSize, httpMethod.length());
}
+ addressSize = Math.max(addressSize, currentAddress.length());
+ methodStrSize = Math.max(methodStrSize, methodToStr.length());
}
- return sb.toString();
- } catch (Exception e) {
- return "<" + e + ">";
+
+ Collections.sort(toLog);
+
+ resourcesToLog.add(new Logs.LogResourceEndpointInfo(type, address,
clazz, toLog, methodSize, methodStrSize));
}
- }
- private static String name(final Type type) {
- if (type instanceof Class<?>) {
- return ((Class) type).getSimpleName() .replace("java.lang.",
"").replace("java.util", "");
- } else if (type instanceof ParameterizedType) {
- final ParameterizedType pt = (ParameterizedType) type;
- final StringBuilder builder = new StringBuilder();
- builder.append(name(pt.getRawType())).append("<");
- for (Type param : pt.getActualTypeArguments()) {
- if (param instanceof Class<?>) {
- builder.append(name(param));
- } else {
- builder.append(param.toString()); // avoid infinite loops
- }
+ Collections.sort(resourcesToLog);
+ for (Logs.LogResourceEndpointInfo resource : resourcesToLog) {
+ LOGGER.info(" Service URI: "
+ + Logs.forceLength(resource.address, addressSize, true) +
" -> "
+ + Logs.forceLength(resource.type, 4, false) + " "
+ + Logs.forceLength(resource.classname, classSize, true));
+
+ for (Logs.LogOperationEndpointInfo log : resource.operations) {
+ LOGGER.info(" "
+ + Logs.forceLength(log.http, resource.methodSize,
false) + " "
+ + Logs.forceLength(log.address, addressSize, true) + "
-> "
+ + Logs.forceLength(log.method, resource.methodStrSize,
true));
}
- builder.append(">");
- return builder.toString();
- }
- return type.toString();
- }
- private static String singleSlash(final String address, final String
value) {
- if (address.endsWith("/") && value.startsWith("/")) {
- return address + value.substring(1);
- }
- if (!address.endsWith("/") && !value.startsWith("/")) {
- return address + '/' + value;
+ resource.operations.clear();
}
- return address + value;
+ resourcesToLog.clear();
}
private JAXRSServerFactoryBean newFactory(String prefix) {
Added:
tomee/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/Logs.java
URL:
http://svn.apache.org/viewvc/tomee/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/Logs.java?rev=1433877&view=auto
==============================================================================
---
tomee/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/Logs.java
(added)
+++
tomee/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/Logs.java
Wed Jan 16 10:04:41 2013
@@ -0,0 +1,186 @@
+/*
+ * 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.openejb.server.cxf.rs;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.List;
+
+public class Logs {
+ public static String forceLength(final String httpMethod, final int l,
final boolean right) {
+ final String http;
+ if (httpMethod == null) { // subresourcelocator implies null http
method
+ http = "";
+ } else {
+ http = httpMethod;
+ }
+
+ final StringBuilder builder = new StringBuilder();
+ if (!right) {
+ for (int i = 0; i < l - http.length(); i++) {
+ builder.append(" ");
+ }
+ }
+ builder.append(http);
+ if (right) {
+ for (int i = 0; i < l - http.length(); i++) {
+ builder.append(" ");
+ }
+ }
+ return builder.toString();
+ }
+
+ public static String toSimpleString(final Method mtd) {
+ try {
+ final StringBuilder sb = new StringBuilder();
+ final Type[] typeparms = mtd.getTypeParameters();
+ if (typeparms.length > 0) {
+ boolean first = true;
+ sb.append("<");
+ for(Type typeparm: typeparms) {
+ if (!first) {
+ sb.append(",");
+ }
+ sb.append(name(typeparm));
+ first = false;
+ }
+ sb.append("> ");
+ }
+
+ final Type genRetType = mtd.getGenericReturnType();
+ sb.append(name(genRetType)).append(" ");
+ sb.append(mtd.getName()).append("(");
+ final Type[] params = mtd.getGenericParameterTypes();
+ for (int j = 0; j < params.length; j++) {
+ sb.append(name(params[j]));
+ if (j < (params.length - 1)) {
+ sb.append(", ");
+ }
+ }
+ sb.append(")");
+ final Type[] exceptions = mtd.getGenericExceptionTypes();
+ if (exceptions.length > 0) {
+ sb.append(" throws ");
+ for (int k = 0; k < exceptions.length; k++) {
+ sb.append(name(exceptions[k]));
+ if (k < (exceptions.length - 1)) {
+ sb.append(",");
+ }
+ }
+ }
+ return sb.toString();
+ } catch (Exception e) {
+ return "<" + e + ">";
+ }
+ }
+
+ public static String name(final Type type) {
+ if (type instanceof Class<?>) {
+ return ((Class) type).getSimpleName() .replace("java.lang.",
"").replace("java.util", "");
+ } else if (type instanceof ParameterizedType) {
+ final ParameterizedType pt = (ParameterizedType) type;
+ final StringBuilder builder = new StringBuilder();
+ builder.append(name(pt.getRawType()));
+ final Type[] args = pt.getActualTypeArguments();
+ if (args != null) {
+ builder.append("<");
+ for (int i = 0; i < args.length; i++) {
+ builder.append(name(args[i]));
+ if (i < args.length - 1) {
+ builder.append(", ");
+ }
+ }
+ builder.append(">");
+ }
+ return builder.toString();
+ }
+ return type.toString();
+ }
+
+ public static String singleSlash(final String address, final String value)
{
+ if (address.endsWith("/") && value.startsWith("/")) {
+ return address + value.substring(1);
+ }
+ if (!address.endsWith("/") && !value.startsWith("/")) {
+ return address + '/' + value;
+ }
+ return address + value;
+ }
+
+ public static class LogOperationEndpointInfo implements
Comparable<LogOperationEndpointInfo> {
+ public final String http;
+ public final String address;
+ public final String method;
+
+ public LogOperationEndpointInfo(final String http, final String
address, final String method) {
+ this.http = http;
+ this.address = address;
+ this.method = method;
+ }
+
+ @Override
+ public int compareTo(final LogOperationEndpointInfo o) {
+ int compare = http.compareTo(o.http);
+ if (compare != 0) {
+ return compare;
+ }
+
+ compare = address.compareTo(o.address);
+ if (compare != 0) {
+ return compare;
+ }
+
+ return method.compareTo(o.method);
+ }
+ }
+
+ public static class LogResourceEndpointInfo implements
Comparable<LogResourceEndpointInfo> {
+ public final String type;
+ public final String address;
+ public final String classname;
+ public final List<LogOperationEndpointInfo> operations;
+ public final int methodSize;
+ public final int methodStrSize;
+
+ public LogResourceEndpointInfo(final String type, final String
address, final String classname,
+ final List<LogOperationEndpointInfo>
operations,
+ final int methodSize, final int
methodStrSize) {
+ this.type = type;
+ this.address = address;
+ this.classname = classname;
+ this.operations = operations;
+ this.methodSize = methodSize;
+ this.methodStrSize = methodStrSize;
+ }
+
+ @Override
+ public int compareTo(final LogResourceEndpointInfo o) {
+ int compare = type.compareTo(o.type);
+ if (compare != 0) {
+ return compare;
+ }
+
+ compare = address.compareTo(o.address);
+ if (compare != 0) {
+ return compare;
+ }
+
+ return classname.compareTo(o.classname);
+ }
+ }
+}