Search in sources :

Example 76 with RefAddr

use of javax.naming.RefAddr in project uavstack by uavorg.

the class JdbcDriverIT method doProxyDataSource.

/**
 * we need inject DataSource, as all DataSource will do there getConnection way which may be not from DriverManager
 *
 * @param obj
 * @param args
 * @param vendor
 * @return
 */
public Object doProxyDataSource(Object dataSourceObj, Object dataSourceCfgObj, String vendor) {
    if (!DataSource.class.isAssignableFrom(dataSourceObj.getClass())) {
        return dataSourceObj;
    }
    String tmpjdbcURL = null;
    if ("Tomcat".equalsIgnoreCase(vendor)) {
        try {
            Reference ref = (Reference) dataSourceCfgObj;
            RefAddr ra = ref.get("url");
            tmpjdbcURL = ra.getContent().toString();
        } catch (Exception e) {
        // ignore
        }
    }
    if (tmpjdbcURL == null) {
        return dataSourceObj;
    }
    this.jdbcUrl = MonitorServerUtil.formatJDBCURL(tmpjdbcURL);
    setTargetServer(tmpjdbcURL);
    DataSource ds = (DataSource) dataSourceObj;
    Set<Class<?>> ifs = ReflectionHelper.getAllInterfaces(dataSourceObj.getClass(), true);
    Class<?>[] dsIfs = new Class<?>[ifs.size()];
    dsIfs = ifs.toArray(dsIfs);
    DataSource dsProxy = JDKProxyInvokeUtil.newProxyInstance(this.getClass().getClassLoader(), dsIfs, new JDKProxyInvokeHandler<DataSource>(ds, new DataSourceProxy()));
    return dsProxy;
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference) SQLException(java.sql.SQLException) DataSource(javax.sql.DataSource)

Example 77 with RefAddr

use of javax.naming.RefAddr in project datanucleus-rdbms by datanucleus.

the class BasicDataSourceFactory method validatePropertyNames.

/**
 * Collects warnings and info messages. Warnings are generated when an obsolete property is set. Unknown properties
 * generate info messages.
 *
 * @param ref
 *            Reference to check properties of
 * @param name
 *            Name provided to getObject
 * @param warnings
 *            container for warning messages
 * @param infoMessages
 *            container for info messages
 */
private void validatePropertyNames(final Reference ref, final Name name, final List<String> warnings, final List<String> infoMessages) {
    final List<String> allPropsAsList = Arrays.asList(ALL_PROPERTIES);
    final String nameString = name != null ? "Name = " + name.toString() + " " : "";
    if (NUPROP_WARNTEXT != null && !NUPROP_WARNTEXT.keySet().isEmpty()) {
        for (final String propertyName : NUPROP_WARNTEXT.keySet()) {
            final RefAddr ra = ref.get(propertyName);
            if (ra != null && !allPropsAsList.contains(ra.getType())) {
                final StringBuilder stringBuilder = new StringBuilder(nameString);
                final String propertyValue = ra.getContent().toString();
                stringBuilder.append(NUPROP_WARNTEXT.get(propertyName)).append(" You have set value of \"").append(propertyValue).append("\" for \"").append(propertyName).append("\" property, which is being ignored.");
                warnings.add(stringBuilder.toString());
            }
        }
    }
    final Enumeration<RefAddr> allRefAddrs = ref.getAll();
    while (allRefAddrs.hasMoreElements()) {
        final RefAddr ra = allRefAddrs.nextElement();
        final String propertyName = ra.getType();
        // and it is not in the "silent" list, tell user we are ignoring it.
        if (!(allPropsAsList.contains(propertyName) || NUPROP_WARNTEXT.keySet().contains(propertyName) || SILENT_PROPERTIES.contains(propertyName))) {
            final String propertyValue = ra.getContent().toString();
            final StringBuilder stringBuilder = new StringBuilder(nameString);
            stringBuilder.append("Ignoring unknown property: ").append("value of \"").append(propertyValue).append("\" for \"").append(propertyName).append("\" property");
            infoMessages.add(stringBuilder.toString());
        }
    }
}
Also used : RefAddr(javax.naming.RefAddr)

Example 78 with RefAddr

use of javax.naming.RefAddr in project iaf by ibissource.

the class TomcatJndiProxy method getObjectInstance.

@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    try {
        // For Tomcat, obj will always be an object of type Reference
        Reference ref = (Reference) obj;
        String objectName = name.toString();
        String targetClassName = ref.getClassName();
        // Class targetClass = Class.forName(targetClassName);
        log.debug("constructing object [{}] of type [{}]", objectName, targetClassName);
        // fetch and set delegate properties
        for (Enumeration<RefAddr> refAddrEnum = ref.getAll(); refAddrEnum.hasMoreElements(); ) {
            RefAddr refAddr = refAddrEnum.nextElement();
            if (refAddr.getType().startsWith(DELEGATE_PREFIX)) {
                String propertyName = refAddr.getType().substring(DELEGATE_PREFIX.length());
                Object propertyValue = refAddr.getContent();
                // log.debug("setting delegate property [{}] to value [{}]", propertyName, propertyValue);
                BeanUtils.setProperty(this, propertyName, propertyValue);
            }
        }
        // if delegate_name is not specified, target object name defaults to 'name'
        String targetObjectName = StringUtils.isNotEmpty(getName()) ? getName() : objectName;
        Context context = getContext();
        Object result = context.lookup(targetObjectName);
        for (Enumeration<RefAddr> refAddrEnum = ref.getAll(); refAddrEnum.hasMoreElements(); ) {
            RefAddr refAddr = refAddrEnum.nextElement();
            String propertyName = refAddr.getType();
            if (!propertyName.startsWith(DELEGATE_PREFIX) && !propertyName.equals("factory")) {
                Object propertyValue = refAddr.getContent();
                // log.debug("setting result property [{}] to value [{}]", propertyName, propertyValue);"
                if (!PropertyUtils.isWriteable(result, propertyName)) {
                    log.warn("object of type [{}] with name [{}] has no property [{}]", result.getClass().getTypeName(), targetObjectName, propertyName);
                }
                BeanUtils.setProperty(result, propertyName, propertyValue);
            }
        }
        log.debug("looked up object [{}]", result);
        return result;
    } catch (Exception e) {
        log.warn("Could not lookup object [{}]", name, e);
        throw e;
    }
}
Also used : RefAddr(javax.naming.RefAddr) Context(javax.naming.Context) Reference(javax.naming.Reference)

Example 79 with RefAddr

use of javax.naming.RefAddr in project wildfly by wildfly.

the class ExceptionMapper method tryFed.

private static NamingException tryFed(NotFound e, CNCtx ctx, NameComponent[] inputName) throws NamingException {
    NameComponent[] rest = ((NotFound) e).rest_of_name;
    if (debug) {
    }
    // If one of those is not found, you get "aa" as 'rest'.
    if (rest.length == 1 && inputName != null) {
        // Check that we're not talking to 1.2/1.3 Sun tnameserv
        NameComponent lastIn = inputName[inputName.length - 1];
        if (rest[0].id.equals(lastIn.id) && rest[0].kind != null && rest[0].kind.equals(lastIn.kind)) {
        // Might be legit
        } else {
            // Due to 1.2/1.3 bug that always returns single-item 'rest'
            NamingException ne = new NameNotFoundException();
            ne.setRemainingName(org.wildfly.iiop.openjdk.naming.jndi.CNNameParser.cosNameToName(rest));
            ne.setRootCause(e);
            throw ne;
        }
    }
    // Fixed in 1.4; perform calculations based on correct (1.4) behavior
    // Calculate the components of the name that has been resolved
    NameComponent[] resolvedName = null;
    int len = 0;
    if (inputName != null && (inputName.length >= rest.length)) {
        if (e.why == NotFoundReason.not_context) {
            // First component of rest is found but not a context; keep it
            // as part of resolved name
            len = inputName.length - (rest.length - 1);
            // Remove resolved component from rest
            if (rest.length == 1) {
                // No more remaining
                rest = null;
            } else {
                NameComponent[] tmp = new NameComponent[rest.length - 1];
                System.arraycopy(rest, 1, tmp, 0, tmp.length);
                rest = tmp;
            }
        } else {
            len = inputName.length - rest.length;
        }
        if (len > 0) {
            resolvedName = new NameComponent[len];
            System.arraycopy(inputName, 0, resolvedName, 0, len);
        }
    }
    // Create CPE and set common fields
    CannotProceedException cpe = new CannotProceedException();
    cpe.setRootCause(e);
    if (rest != null && rest.length > 0) {
        cpe.setRemainingName(org.wildfly.iiop.openjdk.naming.jndi.CNNameParser.cosNameToName(rest));
    }
    cpe.setEnvironment(ctx._env);
    // Lookup resolved name to get resolved object
    final java.lang.Object resolvedObj = (resolvedName != null) ? ctx.callResolve(resolvedName) : ctx;
    if (resolvedObj instanceof javax.naming.Context) {
        // obj is a context and child is not found
        // try getting its nns dynamically by constructing
        // a Reference containing obj.
        RefAddr addr = new RefAddr("nns") {

            public java.lang.Object getContent() {
                return resolvedObj;
            }

            private static final long serialVersionUID = 669984699392133792L;
        };
        Reference ref = new Reference("java.lang.Object", addr);
        // Resolved name has trailing slash to indicate nns
        CompositeName cname = new CompositeName();
        // add trailing slash
        cname.add("");
        cpe.setResolvedObj(ref);
        cpe.setAltName(cname);
        cpe.setAltNameCtx((javax.naming.Context) resolvedObj);
        return cpe;
    } else {
        // Not a context, use object factory to transform object.
        Name cname = org.wildfly.iiop.openjdk.naming.jndi.CNNameParser.cosNameToName(resolvedName);
        java.lang.Object resolvedObj2;
        try {
            resolvedObj2 = NamingManager.getObjectInstance(resolvedObj, cname, ctx, ctx._env);
        } catch (NamingException ge) {
            throw ge;
        } catch (Exception ge) {
            NamingException ne = IIOPLogger.ROOT_LOGGER.errorGeneratingObjectViaFactory();
            ne.setRootCause(ge);
            throw ne;
        }
        // If a context, continue operation with context
        if (resolvedObj2 instanceof javax.naming.Context) {
            cpe.setResolvedObj(resolvedObj2);
        } else {
            // Add trailing slash
            cname.add("");
            cpe.setAltName(cname);
            // Create nns reference
            final java.lang.Object rf2 = resolvedObj2;
            RefAddr addr = new RefAddr("nns") {

                public java.lang.Object getContent() {
                    return rf2;
                }

                private static final long serialVersionUID = -785132553978269772L;
            };
            Reference ref = new Reference("java.lang.Object", addr);
            cpe.setResolvedObj(ref);
            cpe.setAltNameCtx(ctx);
        }
        return cpe;
    }
}
Also used : NamingContext(org.omg.CosNaming.NamingContext) NameComponent(org.omg.CosNaming.NameComponent) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) CompositeName(javax.naming.CompositeName) CannotProceedException(javax.naming.CannotProceedException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) NamingException(javax.naming.NamingException) ContextNotEmptyException(javax.naming.ContextNotEmptyException) InvalidNameException(javax.naming.InvalidNameException) NameNotFoundException(javax.naming.NameNotFoundException) CompositeName(javax.naming.CompositeName) InvalidName(org.omg.CosNaming.NamingContextPackage.InvalidName) Name(javax.naming.Name) RefAddr(javax.naming.RefAddr) CannotProceedException(javax.naming.CannotProceedException) NamingException(javax.naming.NamingException) NotFound(org.omg.CosNaming.NamingContextPackage.NotFound)

Example 80 with RefAddr

use of javax.naming.RefAddr in project tests by datanucleus.

the class DataSourceDriverAdapterObjectFactory method getObjectInstance.

public Object getObjectInstance(Object obj, Name n, Context nameCtx, Hashtable environment) throws Exception {
    Reference ref = (Reference) obj;
    String drivercls = null;
    String driverurl = null;
    String username = null;
    String passwd = null;
    Enumeration addrs = ref.getAll();
    while (addrs.hasMoreElements()) {
        RefAddr addr = (RefAddr) addrs.nextElement();
        String name = addr.getType();
        String value = (String) addr.getContent();
        if (name.equals("driverClassName")) {
            drivercls = value;
        } else if (name.equals("url")) {
            driverurl = value;
        } else if (name.equals("username")) {
            username = value;
        } else if (name.equals("password")) {
            passwd = value;
        }
    }
    Driver drv = (Driver) Class.forName(drivercls).newInstance();
    DataSourceDriverAdapter dsda = new DataSourceDriverAdapter();
    dsda.setDriver(drv);
    dsda.setUrl(driverurl);
    dsda.setUsername(username);
    dsda.setPassword(passwd);
    return dsda;
}
Also used : RefAddr(javax.naming.RefAddr) Enumeration(java.util.Enumeration) Reference(javax.naming.Reference) Driver(java.sql.Driver)

Aggregations

RefAddr (javax.naming.RefAddr)94 Reference (javax.naming.Reference)60 NamingException (javax.naming.NamingException)23 StringRefAddr (javax.naming.StringRefAddr)18 Properties (java.util.Properties)16 ObjectFactory (javax.naming.spi.ObjectFactory)12 InitialContext (javax.naming.InitialContext)9 Method (java.lang.reflect.Method)7 NameNotFoundException (javax.naming.NameNotFoundException)6 EjbRef (org.apache.naming.EjbRef)5 ResourceRef (org.apache.naming.ResourceRef)5 ResourceInfo (org.glassfish.resourcebase.resources.api.ResourceInfo)5 SerializableObjectRefAddr (org.glassfish.resources.naming.SerializableObjectRefAddr)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ArrayList (java.util.ArrayList)4 Enumeration (java.util.Enumeration)4 DataSource (javax.sql.DataSource)4 BeanInfo (java.beans.BeanInfo)3 PropertyDescriptor (java.beans.PropertyDescriptor)3 ResourceLinkRef (org.apache.naming.ResourceLinkRef)3