blob: 404a5aa7e1a0f5ed090e06d2e7b2850dfa6d23f0 [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 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Florian Pirchner - Initial implementation
*/
package org.eclipse.osbp.runtime.web.common.context;
import java.util.Locale;
import java.util.Map;
import org.eclipse.osbp.runtime.common.dispose.AbstractDisposable;
import org.eclipse.osbp.runtime.common.user.IUserInfo;
import org.eclipse.osbp.runtime.web.common.IConstants;
import org.eclipse.osbp.runtime.web.common.IWebContext;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.ComponentInstance;
import org.osgi.service.prefs.Preferences;
import org.osgi.service.prefs.PreferencesService;
// TODO: Auto-generated Javadoc
/**
* The Class AbstractWebContext.
*/
public abstract class AbstractWebContext extends AbstractDisposable implements
IWebContext {
/** The user info. */
private IUserInfo userInfo = null;
/** The id. */
private String id;
/** The preferences service. */
private PreferencesService preferencesService;
/** The properties. */
// private Location userLocation;
private Map<String, Object> properties;
/** The instance. */
private ComponentInstance instance;
/**
* Called by OSGi-DS.
*/
public AbstractWebContext() {
}
/**
* Activate.
*
* @param context
* the context
* @param properties
* the properties
*/
protected void activate(ComponentContext context,
Map<String, Object> properties) {
this.properties = properties;
this.instance = context.getComponentInstance();
id = (String) properties.get(IConstants.OSGI_PROPERTY__WEB_CONTEXT__ID);
// prepare the user info
userInfo = new AbstractUserInfo(
(String) properties
.get(IConstants.OSGI_PROPERTY__WEB_CONTEXT__USER)) {
@Override
public Preferences getPreferences(String qualifier) {
if (preferencesService == null || getId() == null) {
return null;
}
return preferencesService.getUserPreferences(getId());
}
// @Override
// public String getLocation() {
// return System.getProperty("user.home") + "/" + getId();
// }
};
}
/* (non-Javadoc)
* @see org.eclipse.osbp.runtime.web.common.IWebContext#getId()
*/
@Override
public String getId() {
return id;
}
/* (non-Javadoc)
* @see org.eclipse.osbp.runtime.web.common.IWebContext#getUserInfo()
*/
@Override
public IUserInfo getUserInfo() {
return userInfo;
}
/* (non-Javadoc)
* @see org.eclipse.osbp.runtime.web.common.IWebContext#getI18nService()
*/
@Override
public void getI18nService() {
}
/* (non-Javadoc)
* @see org.eclipse.osbp.runtime.web.common.IWebContext#getLocale()
*/
@Override
public Locale getLocale() {
return null;
}
/* (non-Javadoc)
* @see org.eclipse.osbp.runtime.common.dispose.AbstractDisposable#internalDispose()
*/
@Override
protected void internalDispose() {
userInfo = null;
if (instance != null) {
instance.dispose();
}
}
/**
* Called by OSGi-DS.
*/
protected void deactivate() {
instance = null;
if (canDispose()) {
// should not happen for normal. But if the context was deactivated
// by ComponentInstance.dispose() we ensure that WebContext.dispose
// is called too.
dispose();
}
}
/**
* Called by OSGi-DS.
*
* @param service
* the new preferences service
*/
protected void setPreferencesService(PreferencesService service) {
this.preferencesService = service;
}
/**
* Called by OSGi-DS.
*
* @param service
* the service
*/
protected void unsetPreferencesService(PreferencesService service) {
this.preferencesService = null;
}
// /**
// * Called by OSGi-DS
// *
// * @param service
// */
// public void setUserLocation(Location userLocation) {
// this.userLocation = userLocation;
// }
//
// /**
// * Called by OSGi-DS
// *
// * @param service
// */
// public void unsetUserLocation(Location userLocation) {
// this.userLocation = null;
// }
/* (non-Javadoc)
* @see org.eclipse.osbp.runtime.web.common.IWebContext#getProperty(java.lang.String)
*/
@Override
public Object getProperty(String property) {
return properties.get(property);
}
}