View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package com.isenshi.util.extlib;
17  
18  import java.io.File;
19  import java.net.URI;
20  import java.net.URISyntaxException;
21  import java.net.URL;
22  import java.util.Hashtable;
23  
24  import javax.naming.Context;
25  import javax.naming.InitialContext;
26  import javax.naming.NamingException;
27  import javax.naming.directory.Attribute;
28  import javax.naming.directory.BasicAttribute;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.ldap.server.configuration.ShutdownConfiguration;
33  import org.apache.ldap.server.jndi.CoreContextFactory;
34  import org.apache.ldap.server.jndi.ServerContextFactory;
35  import org.seasar.tuigwaa.util.TgwResource;
36  
37  
38  /***
39   * @author someda
40   */
41  public class ApacheDSUtil {
42  		
43  	private static final String WORKING_DIRNAME = "apacheds";	
44  	private static final String SHUTDOWN_PROVIDER_URL = "ou=system";
45  	
46  	private static Log log_ = LogFactory.getLog(ApacheDSUtil.class);
47  	
48  	public static File getWorkingDirectory(){
49  		
50  		URL url = Thread.currentThread().getContextClassLoader().getResource(WORKING_DIRNAME);
51  		File workingdir = null;
52  		
53  		try{
54  			URI uri = new URI(url.toString());
55  			workingdir = new File(uri);			
56  			log_.info("apacheds working directory : " + workingdir);
57  		}catch(URISyntaxException use){
58  			use.printStackTrace();
59  		}
60  	
61  		// try to create working directory
62  		if(!workingdir.exists()){
63  			
64  			
65  		}else if(workingdir.isFile()){
66  			throw new RuntimeException(workingdir.toString() + " must be directory.");			
67  		}
68  				
69  		return workingdir;
70  	}
71  		
72  	/***
73  	 * @param attrname 
74  	 * @return Attribute object with given name
75  	 */
76  	public static Attribute createAttribute(String attrname, String propname){
77  
78  		Attribute attr = new BasicAttribute(attrname);
79  		String values = TgwResource.getProperty(propname);
80  		String[] vals = values.split(",");
81  		for(int i=0;i<vals.length;i++){
82  			attr.add(vals[i]);
83  		}				
84  		return attr;
85  	}	
86  	
87  	public synchronized static void shutdown(Hashtable env) throws NamingException{
88  		
89  		String factory = (String) env.get(Context.INITIAL_CONTEXT_FACTORY);
90  	
91  		if(factory.equals(CoreContextFactory.class.getName()) ||
92  				factory.equals(ServerContextFactory.class.getName())){
93  			
94  			env.put(Context.PROVIDER_URL,SHUTDOWN_PROVIDER_URL);
95  			env.putAll(new ShutdownConfiguration().toJndiEnvironment());
96  			
97  			try{
98  				new InitialContext(env);
99  				log_.info("apacheds shutdown completed.");
100 			}catch(NamingException ne){
101 				log_.error("apacheds shutdown failed.");
102 				throw ne;
103 			}
104 		}		
105 	}
106 }