001    /*
002     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003     *
004     * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
005     *
006     * The contents of this file are subject to the terms of either the GNU
007     * General Public License Version 2 only ("GPL") or the Common Development
008     * and Distribution License("CDDL") (collectively, the "License").  You
009     * may not use this file except in compliance with the License.  You can
010     * obtain a copy of the License at
011     * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
012     * or packager/legal/LICENSE.txt.  See the License for the specific
013     * language governing permissions and limitations under the License.
014     *
015     * When distributing the software, include this License Header Notice in each
016     * file and include the License file at packager/legal/LICENSE.txt.
017     *
018     * GPL Classpath Exception:
019     * Oracle designates this particular file as subject to the "Classpath"
020     * exception as provided by Oracle in the GPL Version 2 section of the License
021     * file that accompanied this code.
022     *
023     * Modifications:
024     * If applicable, add the following below the License Header, with the fields
025     * enclosed by brackets [] replaced by your own identifying information:
026     * "Portions Copyright [year] [name of copyright owner]"
027     *
028     * Contributor(s):
029     * If you wish your version of this file to be governed by only the CDDL or
030     * only the GPL Version 2, indicate your decision by adding "[Contributor]
031     * elects to include this software in this distribution under the [CDDL or GPL
032     * Version 2] license."  If you don't indicate a single choice of license, a
033     * recipient has the option to distribute your version of this file under
034     * either the CDDL, the GPL Version 2 or to extend the choice of license to
035     * its licensees as provided above.  However, if you add GPL Version 2 code
036     * and therefore, elected the GPL Version 2 license, then the option applies
037     * only if the new code is made subject to such option by the copyright
038     * holder.
039     */
040    
041    package com.sun.enterprise.admin.cli;
042    
043    import com.sun.enterprise.util.io.DomainDirs;
044    import com.sun.enterprise.util.io.ServerDirs;
045    import java.io.*;
046    
047    import org.glassfish.api.Param;
048    import org.glassfish.api.admin.*;
049    
050    /**
051     * A class that's supposed to capture all the behavior common to operation
052     * on a "local" domain.  It's supposed to act as the abstract base class that
053     * provides more functionality to the commands that operate on a local domain.
054     * @author केदा&#2352 (km@dev.java.net)
055     * @author Byron Nevins  (bnevins@dev.java.net)
056     */
057    public abstract class LocalDomainCommand extends LocalServerCommand {
058        @Param(name = "domaindir", optional = true)
059        protected String domainDirParam = null;
060        // subclasses decide whether it's optional, required, or not allowed
061        //@Param(name = "domain_name", primary = true, optional = true)
062        private String userArgDomainName;
063        // the key for the Domain Root in the main attributes of the
064        // manifest returned by the __locations command
065        private static final String DOMAIN_ROOT_KEY = "Domain-Root_value";
066        private DomainDirs dd = null;
067    
068        /*
069         * The prepare method must ensure that the superclass' implementation of
070         * the method is called.  
071         * The reason we override here is that we can get into trouble with layers 
072         * of NPE possibilities.  So here the ServerDirs object is initialized
073         * right away.  It will return null for all non-boolean method calls.  But
074         * we never have to do a null-check on the ServerDirs object itself.
075         * ServerDirs is 100% immutable.  A new one will be made later if needed.
076         */
077        @Override
078        protected void prepare()
079                throws CommandException, CommandValidationException {
080            super.prepare();
081            setServerDirs(new ServerDirs()); // do-nothing ServerDirs object...
082        }
083    
084        @Override
085        protected void validate()
086                throws CommandException, CommandValidationException {
087    
088            initDomain();
089        }
090    
091        protected final File getDomainsDir() {
092            return dd.getDomainsDir();
093        }
094    
095        protected final File getDomainRootDir() {
096            return dd.getDomainDir();
097        }
098    
099        protected final String getDomainName() {
100            // can't just use "dd" since it may be half-baked right now!
101    
102            if (dd != null && dd.isValid())
103                return dd.getDomainName();
104            else // too early!
105                return userArgDomainName;  // might be and is ok to be null
106        }
107    
108        /**
109         * We need this so that @Param values for domainname can be remembered later
110         * when the ServerDirs object is made.
111         * @param name the user-specified domain name.
112         */
113        protected final void setDomainName(String name) {
114            dd = null;
115            userArgDomainName = name;
116        }
117    
118        protected void initDomain() throws CommandException {
119            try {
120                File domainsDirFile = null;
121    
122                if (ok(domainDirParam))
123                    domainsDirFile = new File(domainDirParam);
124    
125                dd = new DomainDirs(domainsDirFile, getDomainName());
126                setServerDirs(dd.getServerDirs());
127            } catch (Exception e) {
128                throw new CommandException(e.getMessage(), e);
129            }
130            setLocalPassword();
131        }
132    
133        protected boolean isThisDAS(File ourDir) {
134            return isThisServer(ourDir, DOMAIN_ROOT_KEY);
135        }
136    
137    }