001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.j2ee.j2eeobjectnames;
018    
019    import javax.management.ObjectName;
020    
021    /**
022     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
023     */
024    public class J2eeContextImpl implements J2eeContext {
025    
026        private final String domainName;
027        private final String serverName;
028        private final String applicationName;
029        private final String moduleType;
030        private final String moduleName;
031        private final String j2eeName;
032        private final String j2eeType;
033    
034        public J2eeContextImpl(String domainName, String serverName, String applicationName, String moduleType, String moduleName, String j2eeName, String j2eeType) {
035            this.domainName = domainName;
036            this.serverName = serverName;
037            this.applicationName = applicationName;
038            this.moduleType = moduleType;
039            this.moduleName = moduleName;
040            this.j2eeName = j2eeName;
041            this.j2eeType = j2eeType;
042        }
043    
044        public static J2eeContextImpl newContext(ObjectName source, String moduleType) {
045            return new J2eeContextImpl(source.getDomain(),
046                    source.getKeyProperty(NameFactory.J2EE_SERVER),
047                    source.getKeyProperty(NameFactory.J2EE_APPLICATION),
048                    moduleType,
049                    source.getKeyProperty(moduleType), // <-- This might be wrong!!! GERONIMO-1140
050                    source.getKeyProperty(NameFactory.J2EE_NAME),
051                    source.getKeyProperty(NameFactory.J2EE_TYPE));
052        }
053    
054        /**
055         * This method is a workaround for GERONIMO-1140 -- it's the same as the
056         * previous one except for the offending line.  If this is determined to
057         * be a valid change in general, this can replace newContext(ObjectName, String)
058         * However, I'm not 100% sure that it's OK to have the J2EE_NAME in two
059         * consecutive parameters in the general case.
060         */
061        public static J2eeContextImpl newModuleContext(ObjectName source, String moduleType) {
062            return new J2eeContextImpl(source.getDomain(),
063                    source.getKeyProperty(NameFactory.J2EE_SERVER),
064                    source.getKeyProperty(NameFactory.J2EE_APPLICATION),
065                    moduleType,
066                    source.getKeyProperty(NameFactory.J2EE_NAME),
067                    source.getKeyProperty(NameFactory.J2EE_NAME),
068                    source.getKeyProperty(NameFactory.J2EE_TYPE));
069        }
070    
071        public static J2eeContextImpl newModuleContextFromApplication(ObjectName source, String moduleType, String moduleName) {
072            return new J2eeContextImpl(source.getDomain(),
073                    source.getKeyProperty(NameFactory.J2EE_SERVER),
074                    source.getKeyProperty(NameFactory.J2EE_NAME), //application name in module is name key property in application's object name
075                    moduleType,
076                    moduleName,
077                    null,
078                    null);
079        }
080    
081        public static J2eeContextImpl newModuleContextFromApplication(J2eeContext source, String moduleType, String moduleName) {
082            return new J2eeContextImpl(source.getJ2eeDomainName(),
083                    source.getJ2eeServerName(),
084                    source.getJ2eeApplicationName(),
085                    moduleType,
086                    moduleName,
087                    null,
088                    null);
089        }
090    
091        public String getJ2eeDomainName() {
092            return domainName;
093        }
094    
095        public String getJ2eeServerName() {
096            return serverName;
097        }
098    
099        public String getJ2eeApplicationName() {
100            return applicationName;
101        }
102    
103        public String getJ2eeModuleType() {
104            return moduleType;
105        }
106    
107        public String getJ2eeModuleName() {
108            return moduleName;
109        }
110    
111        public String getJ2eeName() {
112            return j2eeName;
113        }
114    
115        public String getJ2eeType() {
116            return j2eeType;
117        }
118    
119        public String getJ2eeDomainName(String override) {
120            return override == null ? domainName : override;
121        }
122    
123        public String getJ2eeServerName(String override) {
124            return override == null ? serverName : override;
125        }
126    
127        public String getJ2eeApplicationName(String override) {
128            return override == null ? applicationName : override;
129        }
130    
131        public String getJ2eeModuleType(String override) {
132            return override == null ? moduleType : override;
133        }
134    
135        public String getJ2eeModuleName(String override) {
136            return override == null ? moduleName : override;
137        }
138    
139        //most likely the last 2 don't make any sense.
140        public String getJ2eeName(String override) {
141            return override == null ? j2eeName : override;
142        }
143    
144        public String getJ2eeType(String override) {
145            return override == null ? j2eeType : override;
146        }
147    }