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