001/*******************************************************************************
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *******************************************************************************/
014package org.nuxeo.runtime.datasource;
015
016import javax.naming.Context;
017import javax.naming.LinkRef;
018import javax.naming.NamingException;
019
020import org.nuxeo.common.xmap.annotation.XNode;
021import org.nuxeo.common.xmap.annotation.XObject;
022import org.nuxeo.runtime.api.Framework;
023import org.nuxeo.runtime.datasource.PooledDataSourceRegistry.PooledDataSource;
024
025@XObject("link")
026public class DataSourceLinkDescriptor {
027
028    protected String name;
029
030    @XNode("@name")
031    public void setName(String value) {
032        name = DataSourceHelper.getDataSourceJNDIName(value);
033    }
034
035    protected String global;
036
037    @XNode("@global")
038    public void setGlobal(String value) {
039        global = DataSourceHelper.getDataSourceJNDIName(value);
040    }
041
042    @XNode("@type")
043    protected String type;
044
045    public void bindSelf(Context namingContext) throws NamingException {
046        namingContext.bind(name, new LinkRef(global));
047        PooledDataSource pool = DataSourceHelper.getDataSource(global, PooledDataSource.class);
048        Framework.getLocalService(PooledDataSourceRegistry.class).createAlias(DataSourceHelper.relativize(name), pool);
049    }
050
051    public void unbindSelf(Context namingContext) throws NamingException {
052        namingContext.unbind(name);
053    }
054
055}