Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 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.config.support;
import com.sun.enterprise.util.LocalStringManagerImpl;
import com.sun.hk2.component.InhabitantsFile;
import com.sun.hk2.component.InjectionResolver;
import com.sun.logging.LogDomains;
import org.glassfish.api.Param;
import org.glassfish.api.admin.CommandModelProvider;
import org.glassfish.common.util.admin.ParamTokenizer;
import org.jvnet.hk2.annotations.Inject;
import org.jvnet.hk2.annotations.Multiple;
import org.jvnet.hk2.component.ComponentException;
import org.jvnet.hk2.component.Inhabitant;
import org.jvnet.hk2.component.InjectionManager;
import org.jvnet.hk2.component.PostConstruct;
import org.jvnet.hk2.config.*;
import org.jvnet.tiger_types.Types;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* services pertinent to generic CRUD command implementations
*
* @author Jerome Dochez
*
*/
public abstract class GenericCrudCommand implements CommandModelProvider, PostConstruct {
private InjectionResolver injector;
@Inject
Inhabitant> myself;
final protected static Logger logger = LogDomains.getLogger(GenericCrudCommand.class, LogDomains.ADMIN_LOGGER);
final protected static LocalStringManagerImpl localStrings = new LocalStringManagerImpl(GenericCrudCommand.class);
String commandName;
Class parentType=null;
Class targetType=null;
Method targetMethod;
// default level of noise, useful for just swithching these classes in debugging.
protected final Level level = Level.FINE;
public void postConstruct() {
List indexes = myself.metadata().get(InhabitantsFile.INDEX_KEY);
if (indexes.size()!=1) {
StringBuffer sb = new StringBuffer();
for (String index : indexes) {
sb.append(index).append(" ");
}
String msg = localStrings.getLocalString(GenericCrudCommand.class,
"GenericCrudCommand.too_many_indexes",
"The metadata for this generic implementation has more than one index {0}",
sb.toString());
Object[] params = new Object[] { sb.toString()};
logger.log(Level.SEVERE, "GenericCrudCommand.too_many_indexes", params);
throw new ComponentException(msg);
}
String index = indexes.get(0);
if (index.indexOf(":")==-1) {
String msg = localStrings.getLocalString(GenericCrudCommand.class,
"GenericCrudCommand.unamed_service",
"The service {0} is un-named, for generic command, the service name is the command name and must be provided",
index);
Object[] params = new Object[] { index};
logger.log(Level.SEVERE, "GenericCrudCommand.unamed_service", params);
throw new ComponentException(msg);
}
commandName = index.substring(index.indexOf(":")+1);
String parentTypeName = myself.metadata().get(InhabitantsFile.TARGET_TYPE).get(0);
if (logger.isLoggable(level)) {
logger.log(level,"Generic method parent targeted type is " + parentTypeName);
}
try {
parentType = (Class) loadClass(parentTypeName);
} catch(ClassNotFoundException e) {
String msg = localStrings.getLocalString(GenericCrudCommand.class,
"GenericCrudCommand.configbean_not_found",
"The Config Bean {0} cannot be loaded by the generic command implementation : {1}",
parentTypeName, e.getMessage());
Object[] params = new Object[] { parentTypeName, e.getMessage()};
logger.log(Level.SEVERE, "GenericCrudCommand.configbean_not_found",params);
throw new ComponentException(msg, e);
}
// find now the accessor method.
String methodName = myself.metadata().get("method-name").get(0);
targetMethod=null;
for (Method m : parentType.getMethods()) {
if (m.getName().equals(methodName)) {
targetMethod=m;
break;
}
}
if (targetMethod==null) {
String msg = localStrings.getLocalString(GenericCrudCommand.class,
"GenericCrudCommand.configbean_not_found",
"The Config Bean {0} cannot be loaded by the generic command implementation : {1}",
parentTypeName, methodName);
Object[] params = new Object[] { parentTypeName, methodName};
logger.log(Level.SEVERE,"GenericCrudCommand.configbean_not_found", params);
throw new ComponentException(msg);
}
if (targetMethod.getParameterTypes().length==0) {
// return type matters.
targetType = Types.erasure(Types.getTypeArgument(
targetMethod.getGenericReturnType(),0));
} else {
targetType = (Class) targetMethod.getParameterTypes()[0];
}
}
protected T getAnnotation(Method target, Class type) {
T annotation = targetMethod.getAnnotation(type);
if (annotation==null) {
// we need to check for any annotation that has the @Multiple annotation
for (Annotation a : targetMethod.getAnnotations()) {
Multiple multiple = a.annotationType().getAnnotation(Multiple.class);
if (multiple!=null) {
try {
Method m = a.getClass().getMethod("value");
Annotation[] potentials = (Annotation[]) m.invoke(a);
if (potentials!=null) {
for (Annotation potential : potentials) {
if (potential.annotationType().equals(type)) {
m = potential.getClass().getMethod("value");
String value = (String) m.invoke(potential);
if (value.equals(commandName)) {
return type.cast(potential);
}
}
}
}
} catch (Exception e) {
// ignore
}
}
}
String msg = localStrings.getLocalString(GenericCrudCommand.class,
"GenericCrudCommand.annotation_not_found",
"Cannot find annotation {0} with value {1} on method {2}",
type.getName(), commandName, targetMethod.toString());
throw new RuntimeException(msg);
}
return annotation;
}
/**
* we need to have access to the injector instance that has all the parameters context
* @param injector the original command injector
*/
// todo : would be lovely to replace this with some smart injection...
public void setInjectionResolver(InjectionResolver injector) {
this.injector = injector;
}
public InjectionResolver getInjectionResolver() {
final InjectionResolver delegate = injector;
return new InjectionResolver(Param.class) {
@Override
public V getValue(Object component, Inhabitant> onBehalfOf, AnnotatedElement annotated, Type genericType, Class type) throws ComponentException {
if (type.isAssignableFrom(List.class)) {
final List values;
try {
if (annotated instanceof Method) {
values = (List) ((Method) annotated).invoke(component);
} else if (annotated instanceof Field) {
values = (List) ((Field) annotated).get(component);
} else {
String msg = localStrings.getLocalString(GenericCrudCommand.class,
"GenericCrudCommand.invalid_type",
"Invalid annotated type {0} passed to InjectionResolver:getValue()",
annotated.getClass().toString());
Object[] params = new Object[] { annotated.getClass().toString()};
logger.log(Level.SEVERE, "GenericCrudCommand.invalid_type", params);
throw new ComponentException(msg);
}
} catch (IllegalAccessException e) {
String msg = localStrings.getLocalString(GenericCrudCommand.class,
"GenericCrudCommand.invocation_failure",
"Failure {0} while getting List> values from component",
e.getMessage());
Object[] params = new Object[] { e.getMessage()};
logger.log(Level.SEVERE, "GenericCrudCommand.invocation_failure", params);
throw new ComponentException(msg, e);
} catch (InvocationTargetException e) {
String msg = localStrings.getLocalString(GenericCrudCommand.class,
"GenericCrudCommand.invocation_failure",
"Failure {0} while getting List> values from component",
e.getMessage());
Object[] params = new Object[] { e.getMessage()};
logger.log(Level.SEVERE, "GenericCrudCommand.invocation_failure", params);
throw new ComponentException(msg, e);
}
Object value = delegate.getValue(component, null, annotated, genericType, type);
if (value==null) {
if (logger.isLoggable(level)) {
logger.log(level, "Value of " + annotated.toString() + " is null");
}
return null;
}
final Class extends ConfigBeanProxy> itemType = Types.erasure(Types.getTypeArgument(
annotated instanceof Method?
((Method) annotated).getGenericReturnType():((Field) annotated).getGenericType(), 0));
if (logger.isLoggable(level)) {
logger.log(level, "Found that List> really is a List<" + itemType.toString() + ">");
}
if (itemType==null) {
String msg = localStrings.getLocalString(GenericCrudCommand.class,
"GenericCrudCommand.nongeneric_type",
"The List type returned by {0} must be a generic type",
annotated.toString());
Object[] params = new Object[] {annotated.toString()};
logger.log(Level.SEVERE, "GenericCrudCommand.nongeneric_type", params);
throw new ComponentException(msg);
}
if (!ConfigBeanProxy.class.isAssignableFrom(itemType)) {
String msg = localStrings.getLocalString(GenericCrudCommand.class,
"GenericCrudCommand.wrong_type",
"The generic type {0} is not supported, only List extends ConfigBeanProxy> is",
annotated.toString());
Object[] params = new Object[] { annotated.toString()};
logger.log(Level.SEVERE, "GenericCrudCommand.wrong_type", params);
throw new ComponentException(msg);
}
Properties props = convertStringToProperties(value.toString(), ':');
if (logger.isLoggable(level)) {
for (Map.Entry