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.
/*
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
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 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);
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