Author: tommaso
Date: Wed Feb 18 16:52:18 2015
New Revision: 1660672

URL: http://svn.apache.org/r1660672
Log:
OAK-2526 - added persisted configuration and server provider

Added:
    
jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/configuration/nodestate/
    
jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/configuration/nodestate/NodeStateSolrServerProvider.java
   (with props)
    
jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/configuration/nodestate/OakSolrNodeStateConfiguration.java
   (with props)

Added: 
jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/configuration/nodestate/NodeStateSolrServerProvider.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/configuration/nodestate/NodeStateSolrServerProvider.java?rev=1660672&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/configuration/nodestate/NodeStateSolrServerProvider.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/configuration/nodestate/NodeStateSolrServerProvider.java
 Wed Feb 18 16:52:18 2015
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.jackrabbit.oak.plugins.index.solr.configuration.nodestate;
+
+import java.io.IOException;
+import javax.annotation.Nonnull;
+
+import com.google.common.collect.Iterables;
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.api.Type;
+import 
org.apache.jackrabbit.oak.plugins.index.solr.configuration.EmbeddedSolrServerConfiguration;
+import 
org.apache.jackrabbit.oak.plugins.index.solr.configuration.RemoteSolrServerConfiguration;
+import 
org.apache.jackrabbit.oak.plugins.index.solr.configuration.SolrServerConfiguration;
+import 
org.apache.jackrabbit.oak.plugins.index.solr.configuration.SolrServerConfigurationDefaults;
+import 
org.apache.jackrabbit.oak.plugins.index.solr.configuration.SolrServerConfigurationProvider;
+import org.apache.jackrabbit.oak.plugins.index.solr.server.SolrServerProvider;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.apache.solr.client.solrj.SolrServer;
+
+/**
+ * {@link 
org.apache.jackrabbit.oak.plugins.index.solr.server.SolrServerProvider} using 
configuration stored in a repository
+ * node named "server" as a child of a {@code oak:queryIndexDefinition} node 
(e.g. under /../a/b/solrIndex/server)
+ * having {@code type = solr}
+ */
+public class NodeStateSolrServerProvider implements SolrServerProvider {
+
+    private final NodeState nodeState;
+    private SolrServerProvider provider;
+
+    public NodeStateSolrServerProvider(NodeState nodeState) {
+        this.nodeState = nodeState;
+    }
+
+    private int getIntValueFor(String propertyName, int defaultValue) {
+        long value = defaultValue;
+        PropertyState property = nodeState.getProperty(propertyName);
+        if (property != null) {
+            value = property.getValue(Type.LONG);
+        }
+        return (int) value;
+    }
+
+    private String getStringValueFor(String propertyName, String defaultValue) 
{
+        String value = defaultValue;
+        PropertyState property = nodeState.getProperty(propertyName);
+        if (property != null) {
+            value = property.getValue(Type.STRING);
+        }
+        return value;
+    }
+
+    private void checkProviderInitialization() throws IllegalAccessException, 
java.lang.reflect.InvocationTargetException, InstantiationException {
+        synchronized (nodeState) {
+            if (provider == null) {
+                this.provider = new 
NodeStateSolrServerConfigurationProvider().getSolrServerConfiguration().getProvider();
+            }
+        }
+    }
+
+    @Override
+    public SolrServer getSolrServer() throws Exception {
+        checkProviderInitialization();
+        return provider.getSolrServer();
+    }
+
+    @Override
+    public SolrServer getIndexingSolrServer() throws Exception {
+        checkProviderInitialization();
+        return provider.getIndexingSolrServer();
+    }
+
+    @Override
+    public SolrServer getSearchingSolrServer() throws Exception {
+        checkProviderInitialization();
+        return provider.getSearchingSolrServer();
+    }
+
+    private class NodeStateSolrServerConfigurationProvider implements 
SolrServerConfigurationProvider {
+        @Nonnull
+        @Override
+        public SolrServerConfiguration<SolrServerProvider> 
getSolrServerConfiguration() {
+            String type = getStringValueFor(Properties.SERVER_TYPE, 
"embedded");
+            if ("embedded".equalsIgnoreCase(type)) {
+                String solrHomePath = 
getStringValueFor(Properties.SOLRHOME_PATH, 
SolrServerConfigurationDefaults.SOLR_HOME_PATH);
+                String coreName = getStringValueFor(Properties.CORE_NAME, 
SolrServerConfigurationDefaults.CORE_NAME);
+                String context = getStringValueFor(Properties.CONTEXT, null);
+                Integer httpPort = 
Integer.valueOf(getStringValueFor(Properties.HTTP_PORT, "0"));
+
+                if (context != null && httpPort > 0) {
+                    return (SolrServerConfiguration) new 
EmbeddedSolrServerConfiguration(solrHomePath, coreName)
+                            .withHttpConfiguration(context, httpPort);
+                } else {
+                    return (SolrServerConfiguration) new 
EmbeddedSolrServerConfiguration(solrHomePath, coreName);
+                }
+            } else if ("remote".equalsIgnoreCase(type)) {
+                String solrZkHost = getStringValueFor(Properties.ZK_HOST, 
null);
+                String solrCollection = 
getStringValueFor(Properties.COLLECTION, 
SolrServerConfigurationDefaults.COLLECTION);
+                int solrReplicationFactor = 
getIntValueFor(Properties.REPLICATION_FACTOR, 
SolrServerConfigurationDefaults.REPLICATION_FACTOR);
+                String solrConfDir = 
getStringValueFor(Properties.CONFIGURATION_DIRECTORY, 
SolrServerConfigurationDefaults.CONFIGURATION_DIRECTORY);
+                String solrHttpUrls = getStringValueFor(Properties.HTTP_URL, 
SolrServerConfigurationDefaults.HTTP_URL);
+                int solrShardsNo = getIntValueFor(Properties.SHARDS_NO, 
SolrServerConfigurationDefaults.SHARDS_NO);
+
+                return (SolrServerConfiguration) new 
RemoteSolrServerConfiguration(solrZkHost, solrCollection, solrShardsNo,
+                        solrReplicationFactor, solrConfDir, solrHttpUrls);
+            } else {
+                throw new RuntimeException("unexpected Solr server type: " + 
type);
+            }
+        }
+    }
+
+
+    /**
+     * Properties that may be retrieved from the configuration {@link 
org.apache.jackrabbit.oak.spi.state.NodeState}.
+     */
+    public final class Properties {
+        public static final String SERVER_TYPE = "serverType";
+
+        // --> embedded solr server properties <--
+        public static final String SOLRHOME_PATH = "solrHomePath";
+        public static final String CONTEXT = "solrContext";
+        public static final String HTTP_PORT = "httpPort";
+        public static final String CORE_NAME = "coreName";
+
+        // --> remote solr server properties <--
+        public static final String ZK_HOST = "zkHost";
+        public static final String COLLECTION = "collection";
+        public static final String REPLICATION_FACTOR = "replicationFactor";
+        public static final String CONFIGURATION_DIRECTORY = 
"configurationDirectory";
+        public static final String HTTP_URL = "httpUrl";
+        public static final String SHARDS_NO = "shardsNo";
+    }
+
+    @Override
+    public void close() throws IOException {
+        try {
+            getSolrServer().shutdown();
+        } catch (Exception e) {
+            // do nothing
+        } try {
+            getIndexingSolrServer().shutdown();
+        } catch (Exception e) {
+            // do nothing
+        } try {
+            getSearchingSolrServer().shutdown();
+        } catch (Exception e) {
+            // do nothing
+        }
+    }
+
+    @Override
+    public String toString() {
+        return "NodeStateSolrServerProvider{" +
+                "nodeStateChildren=" + 
Iterables.toString(nodeState.getChildNodeNames()) +
+                ", provider=" + provider +
+                '}';
+    }
+}

Propchange: 
jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/configuration/nodestate/NodeStateSolrServerProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/configuration/nodestate/OakSolrNodeStateConfiguration.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/configuration/nodestate/OakSolrNodeStateConfiguration.java?rev=1660672&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/configuration/nodestate/OakSolrNodeStateConfiguration.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/configuration/nodestate/OakSolrNodeStateConfiguration.java
 Wed Feb 18 16:52:18 2015
@@ -0,0 +1,242 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.jackrabbit.oak.plugins.index.solr.configuration.nodestate;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedList;
+import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
+
+import com.google.common.collect.Iterables;
+import org.apache.jackrabbit.oak.api.PropertyState;
+import org.apache.jackrabbit.oak.api.Type;
+import 
org.apache.jackrabbit.oak.plugins.index.solr.configuration.OakSolrConfiguration;
+import 
org.apache.jackrabbit.oak.plugins.index.solr.configuration.SolrServerConfigurationDefaults;
+import org.apache.jackrabbit.oak.spi.query.Filter;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+
+/**
+ * An {@link OakSolrConfiguration} specified via a given {@link 
org.apache.jackrabbit.oak.spi.state.NodeState}.
+ * For each of the supported properties a default is provided if either the
+ * property doesn't exist in the node or if the value is <code>null</code>
+ */
+public class OakSolrNodeStateConfiguration implements OakSolrConfiguration {
+
+    private final NodeState definition;
+
+    public OakSolrNodeStateConfiguration(NodeState definition) {
+        this.definition = definition;
+    }
+
+    @Override
+    public String getFieldNameFor(Type<?> propertyType) {
+        Iterable<String> typeMappings = 
getStringValuesFor(Properties.TYPE_MAPPINGS);
+        if (typeMappings != null) {
+            for (String typeMapping : typeMappings) {
+                String[] mapping = typeMapping.split("=");
+                if (mapping.length == 2 && mapping[0] != null && mapping[1] != 
null) {
+                    Type<?> type = Type.fromString(mapping[0]);
+                    if (type != null && type.tag() == propertyType.tag()) {
+                        return mapping[1];
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    @Nonnull
+    @Override
+    public String getPathField() {
+        return getStringValueFor(Properties.PATH_FIELD, 
SolrServerConfigurationDefaults.PATH_FIELD_NAME);
+    }
+
+    @CheckForNull
+    @Override
+    public String getFieldForPathRestriction(Filter.PathRestriction 
pathRestriction) {
+        String fieldName = null;
+        switch (pathRestriction) {
+            case ALL_CHILDREN: {
+                fieldName = getStringValueFor(Properties.DESCENDANTS_FIELD, 
SolrServerConfigurationDefaults.DESC_FIELD_NAME);
+                break;
+            }
+            case DIRECT_CHILDREN: {
+                fieldName = getStringValueFor(Properties.CHILDREN_FIELD, 
SolrServerConfigurationDefaults.CHILD_FIELD_NAME);
+                break;
+            }
+            case EXACT: {
+                fieldName = getStringValueFor(Properties.PATH_FIELD, 
SolrServerConfigurationDefaults.PATH_FIELD_NAME);
+                break;
+            }
+            case PARENT: {
+                fieldName = getStringValueFor(Properties.PARENT_FIELD, 
SolrServerConfigurationDefaults.ANC_FIELD_NAME);
+                break;
+            }
+            case NO_RESTRICTION:
+                break;
+            default:
+                break;
+
+        }
+        return fieldName;
+    }
+
+    @Override
+    public String getCatchAllField() {
+        return getStringValueFor(Properties.CATCHALL_FIELD, 
SolrServerConfigurationDefaults.CATCHALL_FIELD);
+    }
+
+    @Override
+    public String getFieldForPropertyRestriction(Filter.PropertyRestriction 
propertyRestriction) {
+        Iterable<String> propertyMappings = 
getStringValuesFor(Properties.PROPERTY_MAPPINGS);
+        if (propertyMappings != null) {
+            for (String propertyMapping : propertyMappings) {
+                String[] mapping = propertyMapping.split("=");
+                if (mapping.length == 2 && mapping[0] != null && mapping[1] != 
null) {
+                    if (propertyRestriction.propertyName.equals(mapping[0])) {
+                        return mapping[1];
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    @Nonnull
+    @Override
+    public CommitPolicy getCommitPolicy() {
+        return 
CommitPolicy.valueOf(getStringValueFor(Properties.COMMIT_POLICY, 
CommitPolicy.SOFT.toString()));
+    }
+
+    @Override
+    public int getRows() {
+        return getIntValueFor(Properties.ROWS, 
SolrServerConfigurationDefaults.ROWS);
+    }
+
+    @Override
+    public boolean useForPropertyRestrictions() {
+        return getBooleanValueFor(Properties.PROPERTY_RESTRICIONS, 
SolrServerConfigurationDefaults.PROPERTY_RESTRICTIONS);
+    }
+
+    @Override
+    public boolean useForPrimaryTypes() {
+        return getBooleanValueFor(Properties.PRIMARY_TYPES, 
SolrServerConfigurationDefaults.PRIMARY_TYPES);
+    }
+
+    @Override
+    public boolean useForPathRestrictions() {
+        return getBooleanValueFor(Properties.PATH_RESTRICTIONS, 
SolrServerConfigurationDefaults.PATH_RESTRICTIONS);
+    }
+
+    @Nonnull
+    @Override
+    public Collection<String> getIgnoredProperties() {
+        Collection<String> ignoredProperties;
+        Iterable<String> ignoredPropertiesValues = 
getStringValuesFor(Properties.IGNORED_PROPERTIES);
+        if (ignoredPropertiesValues != null) {
+            ignoredProperties = new LinkedList<String>();
+            for (String ignoredProperty : ignoredPropertiesValues) {
+                ignoredProperties.add(ignoredProperty);
+            }
+        } else {
+            ignoredProperties = Collections.emptyList();
+        }
+        return ignoredProperties;
+    }
+
+    @Nonnull
+    @Override
+    public Collection<String> getUsedProperties() {
+        Collection<String> usedProperties;
+        Iterable<String> usedPropertiesValues = 
getStringValuesFor(Properties.USED_PROPERTIES);
+        if (usedPropertiesValues != null) {
+            usedProperties = new LinkedList<String>();
+            for (String usedProperty : usedPropertiesValues) {
+                usedProperties.add(usedProperty);
+            }
+        } else {
+            usedProperties = Collections.emptyList();
+        }
+        return usedProperties;
+    }
+
+    private boolean getBooleanValueFor(String propertyName, boolean 
defaultValue) {
+        boolean value = defaultValue;
+        PropertyState property = definition.getProperty(propertyName);
+        if (property != null) {
+            value = property.getValue(Type.BOOLEAN);
+        }
+        return value;
+    }
+
+    private int getIntValueFor(String propertyName, int defaultValue) {
+        long value = defaultValue;
+        PropertyState property = definition.getProperty(propertyName);
+        if (property != null) {
+            value = property.getValue(Type.LONG);
+        }
+        return (int) value;
+    }
+
+    private String getStringValueFor(String propertyName, String defaultValue) 
{
+        String value = defaultValue;
+        PropertyState property = definition.getProperty(propertyName);
+        if (property != null) {
+            value = property.getValue(Type.STRING);
+        }
+        return value;
+    }
+
+    private Iterable<String> getStringValuesFor(String propertyName) {
+        Iterable<String> values = null;
+        PropertyState property = definition.getProperty(propertyName);
+        if (property != null && property.isArray()) {
+            values = property.getValue(Type.STRINGS);
+        }
+        return values;
+    }
+
+    @Override
+    public String toString() {
+        return "OakSolrNodeStateConfiguration{" +
+                "definitionChildren=" + 
Iterables.toString(definition.getChildNodeNames()) +
+                '}';
+    }
+
+    /**
+     * Properties that may be retrieved from the configuration {@link 
org.apache.jackrabbit.oak.spi.state.NodeState}.
+     */
+    public final class Properties {
+        // --> oak solr config properties <--
+        public static final String PATH_FIELD = "pathField";
+        public static final String PARENT_FIELD = "parentField";
+        public static final String CHILDREN_FIELD = "childrenField";
+        public static final String DESCENDANTS_FIELD = "descendantsField";
+        public static final String CATCHALL_FIELD = "catchAllField";
+        public static final String COMMIT_POLICY = "commitPolicy";
+        public static final String ROWS = "rows";
+        public static final String PROPERTY_RESTRICIONS = 
"propertyRestrictions";
+        public static final String PRIMARY_TYPES = "primaryTypes";
+        public static final String PATH_RESTRICTIONS = "pathRestrictions";
+        public static final String IGNORED_PROPERTIES = "ignoredProperties";
+        public static final String TYPE_MAPPINGS = "typeMappings";
+        public static final String PROPERTY_MAPPINGS = "propertyMappings";
+        public static final String USED_PROPERTIES = "usedProperties";
+
+    }
+}

Propchange: 
jackrabbit/oak/trunk/oak-solr-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/solr/configuration/nodestate/OakSolrNodeStateConfiguration.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to