001/* 002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others. 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 * 016 * Contributors: 017 * matic 018 */ 019package org.nuxeo.runtime.jtajca; 020 021import javax.naming.Context; 022import javax.naming.InitialContext; 023import javax.naming.NamingException; 024 025/** 026 * Utility class used for testing JNDI space provider. 027 * 028 * @author Stephane Lacoin (aka matic) 029 * @since 5.5 030 */ 031public class InitialContextAccessor extends InitialContext { 032 033 public static final String ENV_CTX_NAME = "java:comp/env"; 034 035 protected InitialContextAccessor() throws NamingException { 036 super(false); // lazy mode is breaking jboss 037 } 038 039 protected Context _getInitCtx() { 040 try { 041 return getURLOrDefaultInitCtx("java:"); 042 } catch (NamingException e) { 043 return null; 044 } 045 } 046 047 /** 048 * Check for JNDI space availability 049 * 050 * @return true if JNDI space exists 051 */ 052 public static boolean isAvailable() { 053 try { 054 return new InitialContextAccessor().getDefaultInitCtx() != null; 055 } catch (NamingException e) { 056 return false; 057 } 058 } 059 060 /** 061 * Try writing in JNDI space 062 * 063 * @param ctx 064 * @return true if JNDI space is writable 065 */ 066 public static boolean isWritable(Context ctx, String prefix) { 067 try { 068 final String name = prefix.concat("IsWritable"); 069 ctx.bind(name, "is-writable"); 070 ctx.unbind(name); 071 } catch (NamingException e) { 072 return false; 073 } 074 return true; 075 } 076 077 /** 078 * Get access to the default initial context implementation 079 * 080 * @return the initial context implementation 081 */ 082 public static Context getInitialContext() { 083 try { 084 return new InitialContextAccessor()._getInitCtx(); 085 } catch (NamingException e) { 086 return null; 087 } 088 } 089 090}