org.glite.authz.pap.ui.cli.policymanagement.Purge.java Source code

Java tutorial

Introduction

Here is the source code for org.glite.authz.pap.ui.cli.policymanagement.Purge.java

Source

/**
 * Copyright (c) Members of the EGEE Collaboration. 2006-2009.
 * See http://www.eu-egee.org/partners/ for details on the copyright holders.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.glite.authz.pap.ui.cli.policymanagement;

import java.rmi.RemoteException;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.glite.authz.pap.ui.cli.CLIException;

public class Purge extends PolicyManagementCLI {

    private static String OPT_PURGE_RESOURCES_LONG = "only-resources";
    private static String OPT_PURGE_RESOURCES_DESCRIPTION = "Remove resources with no actions.";
    private static String OPT_PURGE_ACTIONS_LONG = "only-actions";
    private static String OPT_PURGE_ACTIONS_DESCRIPTION = "Remove actions with no rules";

    private static final String[] commandNameValues = { "purge" };
    private static final String DESCRIPTION = "Remove resources without actions and actions without rules.";
    private static final String USAGE = "[options]";
    private String alias = null;

    public Purge() {
        super(commandNameValues, USAGE, DESCRIPTION, null);
    }

    @SuppressWarnings("static-access")
    @Override
    protected Options defineCommandOptions() {
        Options options = new Options();
        options.addOption(OptionBuilder.hasArg(true).withDescription(OPT_PAPALIAS_DESCRIPTION)
                .withLongOpt(OPT_PAPALIAS_LONG).create());
        options.addOption(OptionBuilder.hasArg(false).withDescription(OPT_PURGE_RESOURCES_DESCRIPTION)
                .withLongOpt(OPT_PURGE_RESOURCES_LONG).create());
        options.addOption(OptionBuilder.hasArg(false).withDescription(OPT_PURGE_ACTIONS_DESCRIPTION)
                .withLongOpt(OPT_PURGE_ACTIONS_LONG).create());
        return options;
    }

    @Override
    protected int executeCommand(CommandLine commandLine) throws CLIException, ParseException, RemoteException {

        String[] args = commandLine.getArgs();

        boolean purgeResources = false;
        boolean purgeActions = false;

        if (commandLine.hasOption(OPT_PURGE_RESOURCES_LONG)) {
            purgeResources = true;
        }

        if (commandLine.hasOption(OPT_PURGE_ACTIONS_LONG)) {
            purgeActions = true;
        }

        if (!(purgeResources || purgeActions)) {
            purgeActions = true;
            purgeResources = true;
        }

        if (args.length > 1) {
            throw new ParseException("Wrong number of arguments: no arguments required.");
        }

        if (commandLine.hasOption(OPT_PAPALIAS_LONG)) {
            alias = commandLine.getOptionValue(OPT_PAPALIAS_LONG);
        }

        if (purgeResources && purgeActions) {
            if (verboseMode) {
                System.out.print("Purging actions and resources... ");
            }
            highlevelPolicyMgmtClient.purge(alias, false, true, false, true);
            if (verboseMode) {
                System.out.println("ok.");
            }
        } else if (purgeResources) {
            if (verboseMode) {
                System.out.print("Purging resources... ");
            }
            highlevelPolicyMgmtClient.purge(alias, false, false, false, true);
            if (verboseMode) {
                System.out.println("ok.");
            }
        } else if (purgeActions) {
            if (verboseMode) {
                System.out.print("Purging actions... ");
            }
            highlevelPolicyMgmtClient.purge(alias, false, true, false, false);
            if (verboseMode) {
                System.out.println("ok.");
            }
        }
        return ExitStatus.SUCCESS.ordinal();
    }
}