blob: 2fd43564e8f620a5c3870a52d06ffa1b609fec0e [file] [log] [blame]
/**
* Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Florian Pirchner - Initial implementation
*/
package org.eclipse.osbp.runtime.web.common.context;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.osbp.runtime.common.dispose.IDisposable;
import org.eclipse.osbp.runtime.web.common.IWebContext;
import org.eclipse.osbp.runtime.web.common.IWebContextRegistry;
import org.osgi.service.component.ComponentFactory;
// TODO: Auto-generated Javadoc
/**
* The Class AbstractWebContextRegistry.
*/
public abstract class AbstractWebContextRegistry implements
IDisposable.Listener, IWebContextRegistry {
/** The factory. */
private ComponentFactory factory;
/** The contexts. */
private Map<String, IWebContext> contexts = Collections
.synchronizedMap(new HashMap<String, IWebContext>());
/**
* Called by OSGi-DS. Sets the component factory that is used to create
* instances of web context.
*
* @param factory
* the new factory
*/
protected void setFactory(ComponentFactory factory) {
this.factory = factory;
}
/**
* Called by OSGi-DS. Unsets the component factory that is used to create
* instances of web context.
*
* @param factory
* the factory
*/
protected void unsetFactory(ComponentFactory factory) {
this.factory = null;
}
/* (non-Javadoc)
* @see org.eclipse.osbp.runtime.web.common.IWebContextRegistry#size()
*/
@Override
public int size() {
return contexts.size();
}
/* (non-Javadoc)
* @see org.eclipse.osbp.runtime.web.common.IWebContextRegistry#getContext(java.lang.String)
*/
@Override
public IWebContext getContext(String id) {
return (IWebContext) (contexts.containsKey(id) ? contexts.get(id)
: null);
}
/* (non-Javadoc)
* @see org.eclipse.osbp.runtime.web.common.IWebContextRegistry#createContext(java.lang.String, java.util.Map)
*/
@Override
public IWebContext createContext(String user, Map<String, Object> properties) {
IWebContext context = doCreateContext(factory, user, properties);
contexts.put(context.getId(), context);
return context;
}
/**
* Delegates the creation of the context to the subclass.
*
* @param factory
* the factory
* @param user
* the user
* @param properties
* the properties
* @return the i web context
*/
protected abstract IWebContext doCreateContext(ComponentFactory factory,
String user, Map<String, Object> properties);
/**
* Notifies that a web context was disposed.
*
* @param notifier
* the notifier
*/
@Override
public void notifyDisposed(IDisposable notifier) {
// remove this as a dispose listener
//
notifier.removeDisposeListener(this);
// look for the context
//
String disposedId = null;
synchronized (contexts) {
for (Map.Entry<String, IWebContext> entry : contexts.entrySet()) {
if (notifier == entry.getValue()) {
disposedId = entry.getKey();
break;
}
}
if (disposedId != null) {
contexts.remove(disposedId);
}
}
}
}