org.glassfish.config.support.GenericListCommand Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of payara-embedded-all Show documentation
Show all versions of payara-embedded-all Show documentation
Payara-Embedded-All Distribution of the Payara Project
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2010-2016 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.config.util.ConfigApiLoggerInfo;
import com.sun.enterprise.util.ColumnFormatter;
import com.sun.enterprise.util.LocalStringManagerImpl;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.api.ActionReport;
import org.glassfish.api.Param;
import org.glassfish.api.admin.AccessRequired;
import org.glassfish.api.admin.AccessRequired.AccessCheck;
import org.glassfish.api.admin.AdminCommand;
import org.glassfish.api.admin.AdminCommandContext;
import org.glassfish.api.admin.AdminCommandSecurity;
import org.glassfish.api.admin.CommandModel;
import org.glassfish.api.logging.LogHelper;
import org.glassfish.common.util.admin.GenericCommandModel;
import org.glassfish.hk2.api.PerLookup;
import org.jvnet.hk2.config.*;
/**
* Generic list command implementation.
*
* @author Jerome Dochez
* @author Tom Mueller
*/
@PerLookup
public class GenericListCommand extends GenericCrudCommand implements AdminCommand, AdminCommandSecurity.AccessCheckProvider {
@Param(primary=true, optional=true)
String name;
@Param(name="long", shortName="l", defaultValue="false", optional=true)
boolean longOpt;
@Param(name="header", shortName="h", defaultValue="true", optional=true)
boolean headerOpt;
@Param(name="output", shortName="o", optional=true)
String outputOpts[];
CommandModel cmdModel;
ConfigModel targetModel;
Listing listing;
// @AccessRequired.To("read")
private ConfigBeanProxy parentBean;
@Override
public void postConstruct() {
super.postConstruct();
listing = targetMethod.getAnnotation(Listing.class);
resolverType = listing.resolver();
try {
// we pass false for "useAnnotations" as the @Param declarations on
// the target type are not used for the List method parameters.
cmdModel = new GenericCommandModel(targetType, false, null, listing.i18n(),
new LocalStringManagerImpl(targetType),
habitat.getService(DomDocument.class), commandName,
false, listing.resolver(), GenericListCommand.class);
targetModel = habitat.getService(DomDocument.class).buildModel(targetType);
if (logger.isLoggable(level)) {
for (String paramName : cmdModel.getParametersNames()) {
CommandModel.ParamModel param = cmdModel.getModelFor(paramName);
logger.log(Level.FINE, "I take {0} parameters", param.getName());
}
}
} catch(Exception e) {
String msg = localStrings.getLocalString(GenericCrudCommand.class,
"GenericCreateCommand.command_model_exception",
"Exception while creating the command model for the generic command {0} : {1}",
commandName, e.getMessage());
LogHelper.log(logger, Level.SEVERE,ConfigApiLoggerInfo.GENERIC_CREATE_CMD_FAILED, e, commandName);
throw new RuntimeException(msg, e);
}
}
@Override
public Collection extends AccessCheck> getAccessChecks() {
final Collection checks = new ArrayList();
checks.add(new AccessCheck(AccessRequired.Util.resourceNameFromConfigBeanProxy(parentBean), "read"));
if (longOpt) {
try {
List children = (List) targetMethod.invoke(parentBean);
for (ConfigBeanProxy child : children) {
if (name == null || name.equals(Dom.unwrap(child).getKey())) {
checks.add(new AccessCheck(AccessRequired.Util.resourceNameFromConfigBeanProxy(child), "read"));
}
}
} catch (Exception ex) {
String msg = localStrings.getLocalString(GenericCrudCommand.class,
"GenericListCommand.accesschecks",
"Exception while creating access checks for generic command {0}: {1}",
commandName, ex.getMessage());
LogHelper.log(logger, Level.SEVERE, ConfigApiLoggerInfo.ACCESS_CHK_CREATE_FAILED, ex, commandName);
throw new RuntimeException(msg, ex);
}
}
return checks;
}
@Override
void prepareInjection(final AdminCommandContext ctx) {
super.prepareInjection(ctx);
parentBean = resolver.resolve(ctx, parentType);
}
@Override
public void execute(final AdminCommandContext context) {
final ActionReport report = context.getActionReport();
if (parentBean==null) {
String msg = localStrings.getLocalString(GenericCrudCommand.class,
"GenericCreateCommand.target_object_not_found",
"The CrudResolver {0} could not find the configuration object of type {1} where instances of {2} should be added",
resolver.getClass().toString(), parentType, targetType);
report.failure(logger, msg);
return;
}
// Force longOpt if output option is specified
if (outputOpts != null) {
longOpt = true;
}
List cols = null;
ColumnFormatter colfm = null;
if (longOpt) {
cols = getColumnInfo(targetType);
if (!isOutputOptsValid(cols, outputOpts)) {
String collist = arrayToString(getColumnHeadings(cols));
String msg = localStrings.getLocalString(GenericCrudCommand.class,
"GenericListCommand.invalidOutputOpts",
"Invalid output option. Choose from the following columns: {0}",
collist);
report.failure(logger, msg);
return;
}
cols = filterColumns(cols, outputOpts);
// sort the columns based on column ordering
Collections.sort(cols, new Comparator() {
@Override
public int compare(ColumnInfo o1, ColumnInfo o2) {
return Integer.compare(o1.order,o2.order);
}
});
colfm = headerOpt ? new ColumnFormatter(getColumnHeadings(cols)) : new ColumnFormatter();
}
List