public void afterPropertiesSet() {
    if (this.target == null) {
      throw new IllegalArgumentException("Property 'target' is required");
    }
    if (this.target instanceof String) {
      throw new IllegalArgumentException(
          "'target' needs to be a bean reference, not a bean name as value");
    }

    ClassLoader proxyClassLoader = target.getClass().getClassLoader();

    ProxyFactory proxyFactory = new ProxyFactory();

    if (this.preInterceptors != null) {
      for (Object interceptor : this.preInterceptors) {
        proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(interceptor));
      }
    }

    // Add the main interceptor (typically an Advisor).
    proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(createMainInterceptor()));

    if (this.postInterceptors != null) {
      for (Object interceptor : this.postInterceptors) {
        proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(interceptor));
      }
    }

    proxyFactory.copyFrom(this);

    TargetSource targetSource = createTargetSource(this.target);
    proxyFactory.setTargetSource(targetSource);

    if (this.proxyInterfaces != null) {
      proxyFactory.setInterfaces(this.proxyInterfaces);
    } else if (!isProxyTargetClass()) {
      // Rely on AOP infrastructure to tell us what interfaces to proxy.
      proxyFactory.setInterfaces(
          ClassUtils.getAllInterfacesForClass(targetSource.getTargetClass(), proxyClassLoader));
    }

    /**
     * use this option to let proxyFactory user cglib to create proxy,otherwise in dynamic script
     * ,this is no dynamic interface 使用这个选荋 将使用cglib 产生代理类,否则在动动的java中,没有动态的接口
     */
    proxyFactory.setOptimize(true);
    this.proxy = proxyFactory.getProxy(proxyClassLoader);
  }
Exemple #2
0
    /**
     * Executes a {@link Work} instance wrapped in two layers of AOP. The first is intended to
     * acquire the proper arguments for {@link Work#doWork(TransactionStatus, Session,
     * ServiceFactory)} for the {@link OmeroContext}, and the second performs all the standard
     * service actions for any normal method call.
     *
     * <p>If the {@link Principal} argument is not null, then additionally, a login/logout sequence
     * will be performed in a try/finally block.
     *
     * @param callContext Possibly null key-value map. See #3529
     * @param p
     * @param work
     */
    public Object execute(
        final Map<String, String> callContext, final Principal p, final Work work) {

      if (work instanceof SimpleWork) {
        ((SimpleWork) work).setSqlAction(sqlAction);
      }

      Interceptor i = new Interceptor(factory);
      ProxyFactory factory = new ProxyFactory();
      factory.setTarget(work);
      factory.setInterfaces(new Class[] {Work.class});

      for (Advice advice : advices) {
        factory.addAdvice(advice);
      }
      factory.addAdvice(i);

      Work wrapper = (Work) factory.getProxy();

      // First we guarantee that this will cause one and only
      // login to take place.
      if (p == null && principalHolder.size() == 0) {
        throw new IllegalStateException("Must provide principal");
      } else if (p != null && principalHolder.size() > 0) {
        throw new IllegalStateException("Already logged in. Use Executor.submit() and .get().");
      }

      // Don't need to worry about the login stack below since
      // already checked.
      if (p != null) {
        this.principalHolder.login(p);
      }
      if (callContext != null) {
        this.principalHolder.setContext(callContext);
      }

      try {
        // Arguments will be replaced after hibernate is in effect
        return wrapper.doWork(null, isf);
      } finally {
        if (callContext != null) {
          this.principalHolder.setContext(null);
        }
        if (p != null) {
          int left = this.principalHolder.logout();
          if (left > 0) {
            log.warn("Logins left: " + left);
            for (int j = 0; j < left; j++) {
              this.principalHolder.logout();
            }
          }
        }
      }
    }
 /**
  * Creates a default transactional proxy for service with default transacction attributes
  *
  * @param <T>
  * @param service
  * @return a Tx Proxy for service with default tx attributes
  */
 @SuppressWarnings("unchecked")
 public <T> PersistentManager<T, Serializable> makeTransactionalProxy(
     PersistentManager<T, Serializable> service) {
   ProxyFactory factory = new ProxyFactory(service);
   factory.setInterfaces(new Class[] {Dao.class});
   TransactionInterceptor interceptor =
       new TransactionInterceptor(transactionManager, new MatchAlwaysTransactionAttributeSource());
   factory.addAdvice(interceptor);
   factory.setTarget(service);
   return (PersistentManager<T, Serializable>) factory.getProxy();
 }
Exemple #4
0
    /**
     * Executes a {@link SqkWork} in transaction.
     *
     * @param work Non-null.
     * @return
     */
    public Object executeSql(final SqlWork work) {

      if (principalHolder.size() > 0) {
        throw new IllegalStateException(
            "Currently logged in. \n"
                + "JDBC will then take part in transaction directly. \n"
                + "Please have the proper JDBC or data source injected.");
      }

      ProxyFactory factory = new ProxyFactory();
      factory.setTarget(work);
      factory.setInterfaces(new Class[] {SqlWork.class});
      factory.addAdvice(advices.get(2)); // TX FIXME
      SqlWork wrapper = (SqlWork) factory.getProxy();
      return wrapper.doWork(this.sqlAction);
    }