org.glassfish.common.util.admin.MapInjectionResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of payara-micro Show documentation
Show all versions of payara-micro Show documentation
Micro Distribution of the Payara Project for IBM JDK
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2013 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.
*/
// Portion Copyright [2017] Payara Foundation and/or affiliates
package org.glassfish.common.util.admin;
import com.sun.enterprise.util.LocalStringManagerImpl;
import java.io.File;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.validation.Validator;
import org.glassfish.api.ExecutionContext;
import org.glassfish.api.Param;
import org.glassfish.api.ParamDefaultCalculator;
import org.glassfish.api.admin.CommandModel;
import org.glassfish.api.admin.ParameterMap;
import org.glassfish.hk2.api.MultiException;
import org.jvnet.hk2.component.MultiMap;
import org.jvnet.hk2.config.InjectionResolver;
/**
* An InjectionResolver that uses a ParameterMap object as the source of
* the data to inject.
*/
public class MapInjectionResolver extends InjectionResolver {
private final CommandModel model;
private final ParameterMap parameters;
private ExecutionContext context = null;
private final MultiMap optionNameToUploadedFileMap;
public static final LocalStringManagerImpl localStrings =
new LocalStringManagerImpl(MapInjectionResolver.class);
private static Validator beanValidator = null;
public MapInjectionResolver(CommandModel model,
ParameterMap parameters) {
this(model, parameters, null);
}
public MapInjectionResolver(CommandModel model,
ParameterMap parameters,
final MultiMap optionNameToUploadedFileMap) {
super(Param.class);
this.model = model;
this.parameters = parameters;
this.optionNameToUploadedFileMap = optionNameToUploadedFileMap;
}
/**
* Set the context that is passed to the DynamicParamImpl.defaultValue method.
*/
public void setContext(ExecutionContext context) {
this.context = context;
}
@Override
public boolean isOptional(AnnotatedElement element, Param annotation) {
String name = CommandModel.getParamName(annotation, element);
CommandModel.ParamModel param = model.getModelFor(name);
return param.getParam().optional();
}
@Override
public V getValue(Object component, AnnotatedElement target, Type genericType, Class type) throws MultiException {
// look for the name in the list of parameters passed.
Param param = target.getAnnotation(Param.class);
String paramName = CommandModel.getParamName(param, target);
if (param.primary()) {
// this is the primary parameter for the command
List value = parameters.get("DEFAULT");
if (value != null && value.size() > 0) {
/*
* If the operands are uploaded files, replace the
* client-provided values with the paths to the uploaded files.
* XXX - assume the lists are in the same order.
*/
final List filePaths = getUploadedFileParamValues(
"DEFAULT",
type, optionNameToUploadedFileMap);
if (filePaths != null) {
value = filePaths;
// replace the file name operands with the uploaded files
parameters.set("DEFAULT", value);
} else {
for (String s : value) {
checkAgainstAcceptableValues(target, s);
}
}
// let's also copy this value to the cmd with a real name
parameters.set(paramName, value);
V paramValue = (V) convertListToObject(target, type, value);
return paramValue;
}
}
if (param.multiple()) {
List value = parameters.get(paramName);
if (value != null && value.size() > 0) {
final List filePaths = getUploadedFileParamValues(
paramName,
type, optionNameToUploadedFileMap);
if (filePaths != null) {
value = filePaths;
// replace the file name operands with the uploaded files
parameters.set(paramName, value);
} else {
for (String s : value) {
checkAgainstAcceptableValues(target, s);
}
}
}
parameters.set(paramName, value);
V paramValue = (V) convertListToObject(target, type, value);
return paramValue;
}
String paramValueStr = getParamValueString(parameters, param, target, context);
/*
* If the parameter is an uploaded file, replace the client-provided
* value with the path to the uploaded file.
*/
final String fileParamValueStr = getUploadedFileParamValue(
paramName, type, optionNameToUploadedFileMap);
if (fileParamValueStr != null) {
paramValueStr = fileParamValueStr;
parameters.set(paramName, paramValueStr);
}
checkAgainstAcceptableValues(target, paramValueStr);
return paramValueStr != null ?
(V) convertStringToObject(target, type, paramValueStr) :
(V) getParamField(component, target);
}
/**
* Returns the path to the uploaded file if the specified field is of type
* File and if the field name is the same as one of the option names that
* was associated with an uploaded File in the incoming command request.
*
* @param fieldName name of the field being injected
* @param fieldType type of the field being injected
* @param optionNameToFileMap map of field names to uploaded Files
* @return absolute path of the uploaded file for the field;
* null if no file uploaded for this field
*/
public static String getUploadedFileParamValue(final String fieldName,
final Class fieldType,
final MultiMap optionNameToFileMap) {
if (optionNameToFileMap == null) {
return null;
}
final File uploadedFile = optionNameToFileMap.getOne(fieldName);
if (uploadedFile != null && fieldType.isAssignableFrom(File.class)) {
return uploadedFile.getAbsolutePath();
} else {
return null;
}
}
/**
* Returns the paths to the uploaded files if the specified field is of type
* File[] and if the field name is the same as one of the option names that
* was associated with an uploaded File in the incoming command request.
*
* @param fieldName name of the field being injected
* @param fieldType type of the field being injected
* @param optionNameToFileMap map of field names to uploaded Files
* @return List of absolute paths of the uploaded files for the field;
* null if no file uploaded for this field
*/
public static List getUploadedFileParamValues(
final String fieldName,
final Class fieldType,
final MultiMap optionNameToFileMap) {
if (optionNameToFileMap == null) {
return null;
}
final List uploadedFiles = optionNameToFileMap.get(fieldName);
if (uploadedFiles != null && uploadedFiles.size() > 0 &&
(fieldType.isAssignableFrom(File.class) ||
fieldType.isAssignableFrom(File[].class))) {
final List paths = new ArrayList(uploadedFiles.size());
for (File f : uploadedFiles)
paths.add(f.getAbsolutePath());
return paths;
} else {
return null;
}
}
/**
* Get the param value. Checks if the param (option) value
* is defined on the command line (URL passed by the client)
* by calling getParameterValue method. If not, then check
* for the shortName. If param value is not given by the
* shortName (short option) then if the default value is
* defined return it.
*
* @param parameters parameters from the command line.
* @param param from the annotated Param
* @param target annotated element
* @return param value
*/
// package-private, for testing
static String getParamValueString(final ParameterMap parameters,
final Param param,
final AnnotatedElement target,
final ExecutionContext context) {
String paramValueStr = getParameterValue(parameters,
CommandModel.getParamName(param, target),
true);
if (paramValueStr == null && param.alias().length() > 0) {
// check for alias
paramValueStr = getParameterValue(parameters, param.alias(), true);
}
if (paramValueStr == null) {
// check for shortName
paramValueStr = parameters.getOne(param.shortName());
}
// if paramValueStr is still null, then check to
// see if the defaultValue is defined
if (paramValueStr == null) {
final String defaultValue = param.defaultValue();
paramValueStr = defaultValue.equals("") ? null : defaultValue;
}
// if paramValueStr is still null, then check to see if a defaultCalculator
// is defined, and if so, call the class to get the default value
if (paramValueStr == null) {
Class extends ParamDefaultCalculator> dc = param.defaultCalculator();
if (dc != ParamDefaultCalculator.class) {
try {
ParamDefaultCalculator pdc = dc.newInstance();
paramValueStr = pdc.defaultValue(context);
} catch (InstantiationException ex) { // @todo Java SE 7 - use multi catch
Logger.getLogger(MapInjectionResolver.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(MapInjectionResolver.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return paramValueStr;
}
/**
* Get the value of the field. This value is defined in the
* annotated Param declaration. For example:
*
* @Param(optional=true)
* String name="server"
*
* The Field, name's value, "server" is returned.
*
* @param component command class object
* @param annotated annotated element
* @return the annotated Field value
*/
// package-private, for testing
static Object getParamField(final Object component,
final AnnotatedElement annotated) {
try {
if (annotated instanceof Field) {
final Field field = (Field)annotated;
AccessController.doPrivileged(new PrivilegedAction
© 2015 - 2024 Weber Informatics LLC | Privacy Policy