|
Glassfish example source code file (ProviderUtil.java)
The Glassfish ProviderUtil.java source code/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright (c) 2009-2010 Oracle and/or its affiliates. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can * obtain a copy of the License at * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html * or packager/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at packager/legal/LICENSE.txt. * * GPL Classpath Exception: * Oracle designates this particular file as subject to the "Classpath" * exception as provided by Oracle in the GPL Version 2 section of the License * file that accompanied this code. * * Modifications: * If applicable, add the following below the License Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyright [year] [name of copyright owner]" * * Contributor(s): * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license." If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. */ package org.glassfish.admin.rest.provider; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.glassfish.admin.rest.Constants; import org.glassfish.admin.rest.ResourceUtil; import org.glassfish.admin.rest.Util; import org.glassfish.admin.rest.results.OptionsResult; import org.glassfish.external.statistics.Statistic; import org.glassfish.external.statistics.impl.StatisticImpl; import org.jvnet.hk2.config.ConfigBean; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.UriInfo; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.Proxy; import java.util.HashMap; import java.util.Iterator; import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; /** * * @author Pajeshwar Patil * @author Ludovic Champenois ludo@dev.java.net */ public class ProviderUtil { public static final String KEY_CHILD_RESOURCE = "childResource"; public static final String KEY_CHILD_RESOURCES = "childResources"; public static final String KEY_COMMAND = "command"; public static final String KEY_COMMANDS = "commands"; public static final String KEY_ENTITY = "entity"; public static final String KEY_METHODS = "methods"; /** * Produce a string in double quotes with backslash sequences in all the * right places. */ public static String quote(String string) { if (string == null || string.length() == 0) { return "\"\""; } char b; char c = 0; int i; int len = string.length(); StringBuilder sb = new StringBuilder(len + 4); String t; sb.append('"'); for (i = 0; i < len; i += 1) { b = c; c = string.charAt(i); switch (c) { case '\\': case '"': sb.append('\\'); sb.append(c); break; case '/': if (b == '<') { sb.append('\\'); } sb.append(c); break; case '\b': sb.append("\\b"); break; case '\t': sb.append("\\t"); break; case '\n': sb.append("\\n"); break; case '\f': sb.append("\\f"); break; case '\r': sb.append("\\r"); break; default: if (c < ' ') { t = "000" + Integer.toHexString(c); sb.append("\\u" + t.substring(t.length() - 4)); } else { sb.append(c); } } } sb.append('"'); return sb.toString(); } static protected final String slashToDash(String string) { if (string != null && !string.isEmpty()) { return string.replaceAll("/", "-"); } else { return string; } } static protected final String readAsString(InputStream in) throws IOException { Reader reader = new InputStreamReader(in); StringBuilder sb = new StringBuilder(); char[] c = new char[1024]; int l; while ((l = reader.read(c)) != -1) { sb.append(c, 0, l); } return sb.toString(); } static public String getElementLink(UriInfo uriInfo, String elementName) { return uriInfo.getRequestUriBuilder().segment(elementName).build().toASCIIString(); } static protected String getStartXmlElement(String name) { assert((name != null) && name.length() > 0); String result ="<"; result = result + name; result = result + ">"; return result; } static protected String getEndXmlElement(String name) { assert((name != null) && name.length() > 0); String result ="<"; result = result + "/"; result = result + name; result = result + ">"; return result; } static public Map getStatistics(Statistic statistic) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { HashMap results = new HashMap(); Class classObject = statistic.getClass(); Method[] methods = classObject.getMethods(); for (Method method: methods) { int modifier = method.getModifiers(); //consider only the public methods if (Modifier.isPublic(modifier)) { String name = method.getName(); //considier only the get* methods if (name.startsWith("get")) { name = name.substring("get".length()); Class<?> returnType = method.getReturnType(); //consider only the methods that return primitives or String objects) if (returnType.isPrimitive() || returnType.getName().equals("java.lang.String")) { results.put(name, method.invoke(statistic, null)); } else { //control should never reach here //we do not expect statistic object to return object //as value for any of it's stats. } } } } return results; } static public Map<String, Object> getStatistic(Statistic statistic) { Map<String,Object> statsMap; // Most likely we will get the proxy of the StatisticImpl, // reconvert that so you can access getStatisticAsMap method if (Proxy.isProxyClass(statistic.getClass())) { statsMap = ((StatisticImpl)Proxy.getInvocationHandler(statistic)).getStaticAsMap(); } else { statsMap = ((StatisticImpl)statistic).getStaticAsMap(); } return statsMap; } static public HashMap<String, String> getStringMap(Map map) { HashMap<String, String> stringMap = new HashMap "; return result; } if (mediaType.equals(MediaType.APPLICATION_JSON)) { result = " " + quote(name) + ":" + jsonValue(value); return result; } if (mediaType.equals(MediaType.APPLICATION_XML)) { result = result + " " + name + "=" + quote(value); return result; } return result; } static public String jsonValue(Object value) { String result =""; if (value.getClass().getName().equals("java.lang.String")) { result = quote(value.toString()); } else { result = value.toString(); } return result; } static public String getHtmlHeader(String baseUri) { String result = "<html>"; result = result + "<title>GlassFish REST Interface"; result = result + getInternalStyleSheet(baseUri); //FIXME - uncomment with the correct link for css file. This external file will override the internal style sheet. ///result = result + "<link rel=\"stylesheet\" type=\"text/css\" href=\"http://localhost:8080/glassfish_rest_interface.css\" />"; result = result + "</head>"; result = result + "<h1 class=\"mainheader\">GlassFish REST Interface"; result = result + "<hr/>"; return result; } static protected JSONArray getJsonForMethodMetaData(OptionsResult metaData) throws JSONException { OptionsResultJsonProvider provider = new OptionsResultJsonProvider(); return provider.getRespresenationForMethodMetaData(metaData); } static protected String getJsonForMethodMetaData(OptionsResult metaData, String indent) { OptionsResultJsonProvider provider = new OptionsResultJsonProvider(); return provider.getRespresenationForMethodMetaData(metaData).toString(); } static protected String getXmlForMethodMetaData(OptionsResult metaData, String indent) { OptionsResultXmlProvider provider = new OptionsResultXmlProvider(); return provider.getRespresenationForMethodMetaData(metaData, indent); } static private String getHtmlRespresentationForParameter(String parameter, ParameterMetaData parameterMetaData) { return getHtmlRespresentationForParameter(parameter, parameterMetaData, null); } static private String getHtmlRespresentationForParameter(String parameter, ParameterMetaData parameterMetaData, String parameterValue) { if ("true".equals(parameterMetaData.getAttributeValue(Constants.DEPRECATED))) { return ""; } String result = parameter; //set appropriate type of input field. In can be of type file or text //file type is used in case of deploy operation String parameterType = parameterMetaData.isFileParameter() ? "file" : "text"; //indicate mandatory field with * super-script if (parameterMetaData.getAttributeValue(Constants.OPTIONAL).equalsIgnoreCase("false")) { result = result + "<sup>*"; } result = "<dt>"; boolean isBoolean = false; if((parameterMetaData.getAttributeValue(Constants.TYPE).endsWith(Constants.JAVA_BOOLEAN_TYPE)) || (parameterMetaData.getAttributeValue(Constants.TYPE).equals(Constants.XSD_BOOLEAN_TYPE))) { isBoolean = true; } boolean hasAcceptableValues = false; String acceptableValues = parameterMetaData.getAttributeValue(Constants.ACCEPTABLE_VALUES); if ((acceptableValues != null) && (acceptableValues.length() > 0)) { hasAcceptableValues = true; } boolean hasValue = false; if ((parameterValue == null) || (parameterValue.equals(""))) { String defaultValue = parameterMetaData.getAttributeValue(Constants.DEFAULT_VALUE); if ((defaultValue != null) && (defaultValue.length() > 0)) { parameterValue = defaultValue; } } if ((parameterValue != null) && (parameterValue.length() > 0)) { hasValue = true; } boolean keyAttribute = Boolean.valueOf(parameterMetaData.getAttributeValue(Constants.KEY)).booleanValue(); if (keyAttribute) { if (hasValue) { result = result + "<dd>"; } else { //no value for the key, so we are in create mode, enable the entry field in the ui //control should never reach here. result = result + "<dd>"; } } else { if (isBoolean || hasAcceptableValues) { //use combo box result = result + "<dd> |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.