FELIX-4545 : Implement Servlet Context Helper

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1655971 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/AbstractInfo.java b/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/AbstractInfo.java
new file mode 100644
index 0000000..85e8bcc
--- /dev/null
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/AbstractInfo.java
@@ -0,0 +1,118 @@
+/*
+ * 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.felix.http.base.internal.runtime;
+
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+
+/**
+ * Base class for all info objects.
+ * Provides support for ranking and ordering of services.
+ */
+public abstract class AbstractInfo<T> implements Comparable<AbstractInfo<T>>
+{
+    /** Service ranking. */
+    private final int ranking;
+
+    /** Service id. */
+    private final long serviceId;
+
+    /** Service id for services not provided through the service registry. */
+    private static final AtomicLong serviceIdCounter = new AtomicLong(-1);
+
+    /** Service reference. */
+    private final ServiceReference<T> serviceReference;
+
+    public AbstractInfo(final ServiceReference<T> ref)
+    {
+        this.serviceId = (Long)ref.getProperty(Constants.SERVICE_ID);
+        final Object rankingObj = ref.getProperty(Constants.SERVICE_RANKING);
+        if ( rankingObj instanceof Integer )
+        {
+            this.ranking = (Integer)rankingObj;
+        }
+        else
+        {
+            this.ranking = 0;
+        }
+        this.serviceReference = ref;
+    }
+
+    public AbstractInfo(final int ranking)
+    {
+        this.ranking = ranking;
+        this.serviceId = serviceIdCounter.getAndDecrement();
+        this.serviceReference = null;
+
+    }
+
+    public AbstractInfo(final int ranking, final long serviceId)
+    {
+        this.ranking = ranking;
+        this.serviceId = serviceId;
+        this.serviceReference = null;
+    }
+
+    /**
+     * Compare two info objects based on their ranking (aka ServiceReference ordering)
+     */
+    @Override
+    public int compareTo(final AbstractInfo<T> other)
+    {
+        if (other.ranking == this.ranking)
+        {
+            if (other.serviceId == this.serviceId)
+            {
+                return 0;
+            }
+            return other.serviceId > this.serviceId ? -1 : 1;
+        }
+
+        return (other.ranking > this.ranking) ? 1 : -1;
+    }
+
+    protected boolean isEmpty(final String value)
+    {
+        return value == null || value.length() == 0;
+    }
+
+    protected String getStringProperty(final ServiceReference<?> ref, final String key)
+    {
+        final Object value = ref.getProperty(key);
+        return (value instanceof String) ? (String) value : null;
+    }
+
+
+    public int getRanking()
+    {
+        return this.ranking;
+    }
+
+    public long getServiceId()
+    {
+        return this.serviceId;
+    }
+
+    public ServiceReference<T> getServiceReference()
+    {
+        return this.serviceReference;
+    }
+}
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/ContextInfo.java b/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/ContextInfo.java
new file mode 100644
index 0000000..d10e7d5
--- /dev/null
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/ContextInfo.java
@@ -0,0 +1,65 @@
+/*
+ * 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.felix.http.base.internal.runtime;
+
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.http.context.ServletContextHelper;
+import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
+
+/**
+ * Provides registration information for a {@link ServletContextHelper}
+ */
+public final class ContextInfo extends AbstractInfo<ServletContextHelper> {
+
+    private final String name;
+
+    private final String path;
+
+    public ContextInfo(final ServiceReference<ServletContextHelper> ref) {
+        super(ref);
+        this.name = this.getStringProperty(ref, HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME);
+        this.path = this.getStringProperty(ref, HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH);
+    }
+
+    /**
+     * Create an info object for the default context.
+     */
+    public ContextInfo()
+    {
+        super(0, Long.MIN_VALUE);
+        this.name = HttpWhiteboardConstants.HTTP_WHITEBOARD_DEFAULT_CONTEXT_NAME;
+        this.path = "/";
+    }
+
+    public boolean isValid()
+    {
+        // TODO - check if name and path are valid values
+        return !this.isEmpty(this.name) && !this.isEmpty(this.path);
+    }
+
+    public String getName()
+    {
+        return this.name;
+    }
+
+    public String getPath()
+    {
+        return this.path;
+    }
+}
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/tracker/AbstractReferenceTracker.java b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/tracker/AbstractReferenceTracker.java
new file mode 100644
index 0000000..acf6e31
--- /dev/null
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/tracker/AbstractReferenceTracker.java
@@ -0,0 +1,62 @@
+/*
+ * 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.felix.http.base.internal.whiteboard.tracker;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Filter;
+import org.osgi.framework.ServiceReference;
+import org.osgi.util.tracker.ServiceTracker;
+
+/**
+ * Service tracker that does not get/unget the service objects itself, but
+ * just forwards the service reference.
+ */
+public abstract class AbstractReferenceTracker<T> extends ServiceTracker<T, ServiceReference<T>>
+{
+    public AbstractReferenceTracker(final BundleContext context, final Filter filter)
+    {
+        super(context, filter, null);
+    }
+
+    @Override
+    public final ServiceReference<T> addingService(final ServiceReference<T> ref)
+    {
+        this.added(ref);
+        return ref;
+    }
+
+    @Override
+    public final void modifiedService(final ServiceReference<T> ref, final ServiceReference<T> service)
+    {
+        this.modified(ref);
+    }
+
+    @Override
+    public final void removedService(final ServiceReference<T> ref, final ServiceReference<T> service)
+    {
+        this.removed(ref);
+    }
+
+    private void modified(final ServiceReference<T> ref) {
+        removed(ref);
+        added(ref);
+    }
+
+    protected abstract void added(final ServiceReference<T> ref);
+
+    protected abstract void removed(final ServiceReference<T> ref);
+}