001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.web.server.impl;
017
018import java.net.URL;
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.HashMap;
022import javax.jcr.Credentials;
023import javax.jcr.SimpleCredentials;
024import javax.servlet.ServletContext;
025import org.modeshape.common.logging.Logger;
026import org.modeshape.jcr.ModeShapeEngine;
027import org.modeshape.jcr.RepositoryConfiguration;
028import org.modeshape.web.shared.RemoteException;
029import org.modeshape.web.server.Connector;
030import org.modeshape.web.server.LRepository;
031import org.modeshape.web.shared.RepositoryName;
032
033/**
034 *
035 * @author kulikov
036 */
037public class JsonConfigConnectorImpl implements Connector {
038    
039    //this object will be serialized by user session but 
040    //we do not want to serialize any data so mark everything as transient
041    private static final long serialVersionUID = 1L;
042    
043    private transient HashMap<String, LRepository> repositories = new HashMap<>();
044    
045    //names of the available repositories
046    private transient Collection<RepositoryName> repositoryNames;
047    
048    //user's credentials
049    private transient Credentials credentials;
050    private transient String userName;
051    
052    //server's env
053    private transient ServletContext context;
054    
055    private transient ModeShapeEngine engine; 
056    private RepositoryList repoList;
057    
058    private final static Logger logger = Logger.getLogger(JsonConfigConnectorImpl.class);
059    
060    public JsonConfigConnectorImpl() {
061    }
062    
063    @Override
064    public void start(ServletContext context) throws RemoteException {
065        String urlString = context.getInitParameter("config-url");
066        
067        repositoryNames = new ArrayList<>();
068        
069        engine = new ModeShapeEngine();
070        engine.start();
071                
072        try {
073            //we are using class loader to get the URL inside war
074            URL configURL = getClass().getClassLoader().getResource(urlString);
075            RepositoryConfiguration config = RepositoryConfiguration.read(configURL);
076            engine.deploy(config);
077            repoList = new RepositoryList(engine);
078            repositoryNames = repoList.getRepositories(null);
079        } catch (Exception e) {
080            throw new RemoteException(e.getMessage());
081        }
082    }
083    
084    @Override
085    public void login( String username, String password ) throws RemoteException {        
086        this.userName = username;
087        if (username == null) {
088            credentials = null;
089        }
090        
091        if (password == null) {
092            credentials = new SimpleCredentials(username, null);
093        } else {
094            credentials = new SimpleCredentials(username, password.toCharArray());
095        }
096        repositoryNames = repoList.getRepositories(credentials);        
097    }
098
099    @Override
100    public void logout() {
101        credentials = null;
102        userName = null;
103        try {
104            repositoryNames = repoList.getRepositories(null);
105        } catch (RemoteException e) {
106            repositoryNames.clear();
107        }
108    }
109    
110    @Override
111    public String userName() {
112        return userName;
113    }
114    
115    @Override
116    public Collection<RepositoryName> getRepositories() {
117        return repositoryNames;
118    }
119
120    @Override
121    public LRepository find(String name) throws RemoteException {
122        if (!repositories.containsKey(name)) {
123            try {
124                logger.debug("Starting repository: " + name);
125                repositories.put(name, 
126                        new LRepositoryImpl(context,
127                        engine.getRepository(name), credentials));
128            } catch (Exception e) {
129                logger.debug("Could not start repository " + name, e);
130                throw new RemoteException(e.getMessage());
131            }
132        }
133        return repositories.get(name);
134    }
135
136    @Override
137    public Collection<RepositoryName> search(String name) {
138        ArrayList<RepositoryName> list = new ArrayList<>();
139        for (RepositoryName n : repositoryNames) {
140            if (n.getName().contains(name) || n.getDescriptor().contains(name)) {
141                list.add(n);
142            }
143        }
144        return list;
145    }
146    
147    
148}