selenium.server/src/org/netbeans/modules/selenium/server/SeleniumProperties.java
author Martin Fousek <marfous@netbeans.org>
Thu, 01 Nov 2012 09:53:16 +0100
branchrelease72
changeset 17894 7ceebbb201a7
parent 17795 ed07e23ee4d1
permissions -rw-r--r--
#217956 - UI Element / User-extensions are not configurable via Selenium Server Configuartion
jsedek@14783
     1
/*
jsedek@14783
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jsedek@14783
     3
 *
jsedek@14783
     4
 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
jsedek@14783
     5
 *
jsedek@14783
     6
 * The contents of this file are subject to the terms of either the GNU
jsedek@14783
     7
 * General Public License Version 2 only ("GPL") or the Common
jsedek@14783
     8
 * Development and Distribution License("CDDL") (collectively, the
jsedek@14783
     9
 * "License"). You may not use this file except in compliance with the
jsedek@14783
    10
 * License. You can obtain a copy of the License at
jsedek@14783
    11
 * http://www.netbeans.org/cddl-gplv2.html
jsedek@14783
    12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jsedek@14783
    13
 * specific language governing permissions and limitations under the
jsedek@14783
    14
 * License.  When distributing the software, include this License Header
jsedek@14783
    15
 * Notice in each file and include the License file at
jsedek@14783
    16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jsedek@14783
    17
 * particular file as subject to the "Classpath" exception as provided
jsedek@14783
    18
 * by Sun in the GPL Version 2 section of the License file that
jsedek@14783
    19
 * accompanied this code. If applicable, add the following below the
jsedek@14783
    20
 * License Header, with the fields enclosed by brackets [] replaced by
jsedek@14783
    21
 * your own identifying information:
jsedek@14783
    22
 * "Portions Copyrighted [year] [name of copyright owner]"
jsedek@14783
    23
 *
jsedek@14783
    24
 * If you wish your version of this file to be governed by only the CDDL
jsedek@14783
    25
 * or only the GPL Version 2, indicate your decision by adding
jsedek@14783
    26
 * "[Contributor] elects to include this software in this distribution
jsedek@14783
    27
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jsedek@14783
    28
 * single choice of license, a recipient has the option to distribute
jsedek@14783
    29
 * your version of this file under either the CDDL, the GPL Version 2 or
jsedek@14783
    30
 * to extend the choice of license to its licensees as provided above.
jsedek@14783
    31
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jsedek@14783
    32
 * Version 2 license, then the option applies only if the new code is
jsedek@14783
    33
 * made subject to such option by the copyright holder.
jsedek@14783
    34
 *
jsedek@14783
    35
 * Contributor(s):
jsedek@14783
    36
 *
jsedek@14783
    37
 * Portions Copyrighted 2009 Sun Microsystems, Inc.
jsedek@14783
    38
 */
jsedek@14783
    39
package org.netbeans.modules.selenium.server;
jsedek@14783
    40
jsedek@14783
    41
import java.beans.PropertyChangeEvent;
jsedek@14783
    42
import java.lang.reflect.InvocationTargetException;
marfous@17590
    43
import java.net.URLClassLoader;
jsedek@14783
    44
import java.util.List;
marfous@17590
    45
import java.util.logging.Level;
marfous@17590
    46
import java.util.logging.Logger;
jsedek@14783
    47
import org.netbeans.api.server.properties.InstanceProperties;
jsedek@14783
    48
import org.netbeans.api.server.properties.InstancePropertiesManager;
jsedek@14783
    49
import org.openide.nodes.Node;
jsedek@14783
    50
import org.openide.nodes.Node.Property;
jsedek@14783
    51
import org.openide.nodes.Sheet;
jsedek@14783
    52
import org.openide.nodes.Sheet.Set;
jsedek@14783
    53
import org.openide.util.NbBundle;
jsedek@14783
    54
jsedek@14783
    55
/**
jsedek@14783
    56
 *
jsedek@14783
    57
 * @author Jindrich Sedek
marfous@17590
    58
 * @author Martin Fousek
jsedek@14783
    59
 */
jsedek@14783
    60
public class SeleniumProperties {
jsedek@14783
    61
marfous@17590
    62
    private static Logger LOGGER = Logger.getLogger(SeleniumProperties.class.getName());
marfous@17590
    63
marfous@17590
    64
    public static int seleniumDefaultPort = -1;
marfous@17795
    65
    public static final String PORT = "Port"; //NOI18N
marfous@17795
    66
    public static final String START_ON_STARTUP = "Startup"; //NOI18N
marfous@17795
    67
    public static final String FIREFOX_PROFILE = "FirefoxProfile"; //NOI18N
marfous@17795
    68
    public static final String SINGLE_WINDOW = "SingleWindow"; //NOI18N
marfous@17893
    69
    public static final String USER_EXTENSIONS = "UserExtensions"; //NOI18N
marfous@17795
    70
    
jsedek@14783
    71
    private static InstanceProperties instanceProps;
jsedek@14783
    72
    private static final String NAMESPACE = "Selenium server properties namespace"; //NOI18N
jsedek@14783
    73
jsedek@14783
    74
    static Sheet createSheet() {
jsedek@14783
    75
        InstanceProperties props = getInstanceProperties();
jsedek@14783
    76
        Sheet sheet = Sheet.createDefault();
jsedek@14783
    77
        Set set = sheet.get(Sheet.PROPERTIES);
jsedek@14783
    78
        set.put(new ServerIntProperty(PORT, props));
jsedek@14783
    79
        set.put(new ServerBoolProperty(START_ON_STARTUP, props));
marfous@17795
    80
        set.put(new ServerStringProperty(FIREFOX_PROFILE, props));
marfous@17795
    81
        set.put(new ServerBoolProperty(SINGLE_WINDOW, props));
marfous@17893
    82
        set.put(new ServerStringProperty(USER_EXTENSIONS, props));
jsedek@14783
    83
        return sheet;
jsedek@14783
    84
    }
jsedek@14783
    85
marfous@17590
    86
    /**
marfous@17590
    87
     * Gets the default server port contained in the Selenium server configuration.
marfous@17590
    88
     * @return default Selenium server port
marfous@17590
    89
     */
marfous@17590
    90
    public static int getSeleniumDefaultPort() {
marfous@17590
    91
        if (seleniumDefaultPort == -1) {
marfous@17590
    92
            try {
marfous@17590
    93
                URLClassLoader urlClassLoader = SeleniumServerRunner.getSeleniumServerClassLoader();
marfous@17590
    94
                Class remoteControlConfiguration = urlClassLoader.loadClass(
marfous@17590
    95
                        "org.openqa.selenium.server.RemoteControlConfiguration"); //NOI18N
marfous@17590
    96
                seleniumDefaultPort = remoteControlConfiguration.getDeclaredField(
marfous@17590
    97
                        "DEFAULT_PORT").getInt(remoteControlConfiguration); //NOI18N
marfous@17590
    98
            } catch (NoSuchFieldException ex) {
marfous@17590
    99
                LOGGER.log(Level.SEVERE, null, ex);
marfous@17590
   100
            } catch (SecurityException ex) {
marfous@17590
   101
                LOGGER.log(Level.SEVERE, null, ex);
marfous@17590
   102
            } catch (IllegalArgumentException ex) {
marfous@17590
   103
                LOGGER.log(Level.SEVERE, null, ex);
marfous@17590
   104
            } catch (IllegalAccessException ex) {
marfous@17590
   105
                LOGGER.log(Level.SEVERE, null, ex);
marfous@17590
   106
            } catch (ClassNotFoundException ex) {
marfous@17590
   107
                LOGGER.log(Level.SEVERE, null, ex);
marfous@17590
   108
            }
marfous@17590
   109
        }
marfous@17590
   110
        return seleniumDefaultPort;
marfous@17590
   111
    }
marfous@17590
   112
jsedek@14783
   113
    public static InstanceProperties getInstanceProperties(){
jsedek@14783
   114
        if (instanceProps == null){
jsedek@14783
   115
            InstancePropertiesManager manager = InstancePropertiesManager.getInstance();
jsedek@15925
   116
            synchronized (NAMESPACE){
jsedek@15925
   117
                List<InstanceProperties> allProps = manager.getProperties(NAMESPACE);
jsedek@15925
   118
                if (!allProps.isEmpty()) {
jsedek@15925
   119
                    instanceProps = allProps.iterator().next();
jsedek@15925
   120
                } else {
jsedek@15925
   121
                    instanceProps = manager.createProperties(NAMESPACE);
marfous@17590
   122
                    instanceProps.putInt(PORT, getSeleniumDefaultPort());
jsedek@15925
   123
                    instanceProps.putBoolean(START_ON_STARTUP, true);
marfous@17795
   124
                    instanceProps.putString(FIREFOX_PROFILE, ""); //NOI18N
marfous@17795
   125
                    instanceProps.putBoolean(SINGLE_WINDOW, false);
marfous@17893
   126
                    instanceProps.putString(USER_EXTENSIONS, ""); //NOI18N
jsedek@15925
   127
                    allProps.add(instanceProps);
jsedek@15925
   128
                }
jsedek@14783
   129
            }
jsedek@14783
   130
        }
jsedek@14783
   131
        return instanceProps;
jsedek@14783
   132
    }
jsedek@14783
   133
jsedek@14783
   134
    private static final class ServerBoolProperty extends ServerProperty<Boolean> {
jsedek@14783
   135
jsedek@14783
   136
        public ServerBoolProperty(String propertyName, InstanceProperties props) {
jsedek@14783
   137
            super(Boolean.class, propertyName, props);
jsedek@14783
   138
        }
jsedek@14783
   139
jsedek@14783
   140
        @Override
jsedek@14783
   141
        public Boolean getValue() throws IllegalAccessException, InvocationTargetException {
jsedek@14783
   142
            return props.getBoolean(getName(), true);
jsedek@14783
   143
        }
jsedek@14783
   144
jsedek@14783
   145
        @Override
jsedek@14783
   146
        public void setValue(Boolean val) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
jsedek@14783
   147
            Boolean oldValue = getValue();
jsedek@14783
   148
            if (oldValue.equals(val)){
jsedek@14783
   149
                return;
jsedek@14783
   150
            }
jsedek@14783
   151
            props.putBoolean(getName(), val);
jsedek@14783
   152
        }
jsedek@14783
   153
jsedek@14783
   154
    }
jsedek@14783
   155
jsedek@14783
   156
    private static final class ServerIntProperty extends ServerProperty<Integer> {
jsedek@14783
   157
jsedek@14783
   158
        public ServerIntProperty(String propertyName, InstanceProperties props) {
jsedek@14783
   159
            super(Integer.class, propertyName, props);
jsedek@14783
   160
        }
jsedek@14783
   161
jsedek@14783
   162
        @Override
jsedek@14783
   163
        public Integer getValue() throws IllegalAccessException, InvocationTargetException {
jsedek@14783
   164
            return props.getInt(getName(), 0);
jsedek@14783
   165
        }
jsedek@14783
   166
jsedek@14783
   167
        @Override
jsedek@14783
   168
        protected void writeNewValue(Integer val) {
jsedek@14783
   169
            props.putInt(getName(), val);
jsedek@14783
   170
        }
jsedek@14783
   171
jsedek@14783
   172
    }
jsedek@14783
   173
marfous@17795
   174
    private static final class ServerStringProperty extends ServerProperty<String> {
jsedek@14783
   175
marfous@17795
   176
        public ServerStringProperty(String propertyName, InstanceProperties props) {
marfous@17795
   177
            super(String.class, propertyName, props);
marfous@17795
   178
        }
marfous@17795
   179
marfous@17795
   180
        @Override
marfous@17795
   181
        public String getValue() throws IllegalAccessException, InvocationTargetException {
marfous@17795
   182
            return props.getString(getName(), ""); //NOI18N
marfous@17795
   183
        }
marfous@17795
   184
marfous@17795
   185
        @Override
marfous@17795
   186
        protected void writeNewValue(String val) {
marfous@17795
   187
            props.putString(getName(), val);
marfous@17795
   188
        }
marfous@17795
   189
marfous@17795
   190
    }
jsedek@14783
   191
jsedek@14783
   192
    private static abstract class ServerProperty<T> extends Node.Property<T>{
jsedek@14783
   193
jsedek@14783
   194
        protected InstanceProperties props;
jsedek@14783
   195
jsedek@14783
   196
        public ServerProperty(Class<T> type, String propertyName, InstanceProperties props) {
jsedek@14783
   197
            super(type);
jsedek@14783
   198
            this.props = props;
jsedek@14783
   199
            setName(propertyName);
jsedek@14783
   200
            setDisplayName(NbBundle.getMessage(SeleniumProperties.class, "displayName_" + propertyName));
jsedek@14783
   201
            setShortDescription(NbBundle.getMessage(SeleniumProperties.class, "desc_" + propertyName));
jsedek@14783
   202
        }
jsedek@14783
   203
jsedek@14783
   204
        @Override
jsedek@14783
   205
        public boolean canRead() {
jsedek@14783
   206
            return true;
jsedek@14783
   207
        }
jsedek@14783
   208
jsedek@14783
   209
        @Override
jsedek@14783
   210
        public boolean canWrite() {
jsedek@14783
   211
            return true;
jsedek@14783
   212
        }
jsedek@14783
   213
jsedek@14783
   214
        @Override
jsedek@14783
   215
        public void setValue(T val) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
jsedek@14783
   216
            T oldValue = getValue();
jsedek@14783
   217
            if (oldValue.equals(val)){
jsedek@14783
   218
                return;
jsedek@14783
   219
            }
jsedek@14783
   220
            writeNewValue(val);
jsedek@14783
   221
            PropertyChangeEvent evt = new PropertyChangeEvent(this, getName(), oldValue, val);
jsedek@14783
   222
            SeleniumServerRunner.getPropertyChangeListener().propertyChange(evt);
jsedek@14783
   223
        }
jsedek@14783
   224
jsedek@14783
   225
        protected void writeNewValue(T val){}
jsedek@14783
   226
jsedek@14783
   227
    }
jsedek@14783
   228
}
jsedek@14783
   229
jsedek@14783
   230