예제 #1
0
  public static DatabaseException notFoundException(PageContext pc, String datasource) {

    List<String> list = new ArrayList<String>();

    // application based datasources
    DataSource[] datasources =
        ((ApplicationContextPro) pc.getApplicationContext()).getDataSources();
    if (datasources != null)
      for (int i = 0; i < datasources.length; i++) {
        list.add(datasources[i].getName());
      }

    // config based datasources
    datasources = pc.getConfig().getDataSources();
    if (datasources != null)
      for (int i = 0; i < datasources.length; i++) {
        list.add(datasources[i].getName());
      }

    // create error detail
    DatabaseException de =
        new DatabaseException("datasource [" + datasource + "] doesn't exist", null, null, null);
    de.setDetail(
        ExceptionUtil.createSoundexDetail(datasource, list.iterator(), "datasource names"));
    de.setAdditional(KeyConstants._Datasource, datasource);
    return de;
  }
예제 #2
0
  /**
   * @param pc
   * @param session
   * @param entityName name of the entity to get
   * @param unique create a unique version that can be manipulated
   * @param init call the nit method of the cfc or not
   * @return
   * @throws PageException
   */
  public Component create(
      PageContext pc, HibernateORMSession session, String entityName, boolean unique)
      throws PageException {

    // get existing entity
    ComponentPro cfc = _create(pc, entityName, unique);
    if (cfc != null) return cfc;

    // reinit ORMEngine
    SessionFactory _old = _factory;
    SessionFactory _new = getSessionFactory(pc, true);
    if (_old != _new) {
      session.resetSession(_new);
      cfc = _create(pc, entityName, unique);
      if (cfc != null) return cfc;
    }

    ApplicationContextPro appContext = ((ApplicationContextPro) pc.getApplicationContext());
    ORMConfiguration ormConf = appContext.getORMConfiguration();
    Resource[] locations = ormConf.getCfcLocations();

    throw new ORMException(
        "No entity (persitent component) with name ["
            + entityName
            + "] found, available entities are ["
            + railo.runtime.type.List.arrayToList(getEntityNames(), ", ")
            + "] ",
        "component are searched in the following directories [" + toString(locations) + "]");
  }
예제 #3
0
 @Override
 public void onSessionStart(PageContext pc) throws PageException {
   ComponentAccess app = apps.get(pc.getApplicationContext().getName());
   if (hasOnSessionStart(pc, app)) {
     call(app, pc, ON_SESSION_START, ArrayUtil.OBJECT_EMPTY, true);
   }
 }
예제 #4
0
 /** @see railo.runtime.orm.ORMEngine#getConfiguration(railo.runtime.PageContext) */
 public ORMConfiguration getConfiguration(PageContext pc) {
   ApplicationContext ac = pc.getApplicationContext();
   if (!(ac instanceof ApplicationContextPro)) return null;
   ApplicationContextPro acp = (ApplicationContextPro) ac;
   if (!acp.isORMEnabled()) return null;
   return acp.getORMConfiguration();
 }
예제 #5
0
 @Override
 public boolean onApplicationStart(PageContext pc) throws PageException {
   ComponentAccess app = apps.get(pc.getApplicationContext().getName());
   if (app != null && app.contains(pc, ON_APPLICATION_START)) {
     Object rtn = call(app, pc, ON_APPLICATION_START, ArrayUtil.OBJECT_EMPTY, true);
     return Caster.toBooleanValue(rtn, true);
   }
   return true;
 }
예제 #6
0
 @Override
 public void onDebug(PageContext pc) throws PageException {
   if (((PageContextImpl) pc).isGatewayContext()) return;
   ComponentAccess app = apps.get(pc.getApplicationContext().getName());
   if (app != null && app.contains(pc, ON_DEBUG)) {
     call(app, pc, ON_DEBUG, new Object[] {pc.getDebugger().getDebuggingData(pc)}, true);
     return;
   }
   try {
     pc.getDebugger().writeOut(pc);
   } catch (IOException e) {
     throw Caster.toPageException(e);
   }
 }
예제 #7
0
  public boolean reload(PageContext pc, boolean force) throws PageException {
    if (force) {
      if (_factory != null) {
        _factory.close();
        _factory = null;
        configuration = null;
      }
    } else {
      Object h = hash((ApplicationContextPro) pc.getApplicationContext());
      if (this.hash.equals(h)) return false;
    }

    getSessionFactory(pc, true);
    return true;
  }
예제 #8
0
  @Override
  public void onError(PageContext pc, PageException pe) {
    ComponentAccess app = apps.get(pc.getApplicationContext().getName());
    if (app != null && app.containsKey(ON_ERROR) && !Abort.isSilentAbort(pe)) {
      try {
        String eventName = "";
        if (pe instanceof ModernAppListenerException)
          eventName = ((ModernAppListenerException) pe).getEventName();
        if (eventName == null) eventName = "";

        call(app, pc, ON_ERROR, new Object[] {pe.getCatchBlock(pc), eventName}, true);
        return;
      } catch (PageException _pe) {
        pe = _pe;
      }
    }
    pc.handlePageException(pe);
  }
예제 #9
0
  /** @see railo.runtime.orm.ORMEngine#getSession(railo.runtime.PageContext) */
  public ORMSession createSession(PageContext pc) throws PageException {
    ApplicationContextPro appContext = ((ApplicationContextPro) pc.getApplicationContext());
    String dsn = appContext.getORMDatasource();

    // DatasourceManager manager = pc.getDataSourceManager();
    // DatasourceConnection dc=manager.getConnection(pc,dsn, null, null);
    DatasourceConnection dc =
        ((ConfigWebImpl) pc.getConfig())
            .getDatasourceConnectionPool()
            .getDatasourceConnection(pc, pc.getConfig().getDataSource(dsn), null, null);
    try {

      return new HibernateORMSession(this, getSessionFactory(pc), dc);
    } catch (PageException pe) {
      // manager.releaseConnection(pc, dc);// connection is closed when session ends
      throw pe;
    }
  }
예제 #10
0
  protected void _onRequest(
      PageContext pc, PageSource requestedPage, PageSource appPS, RequestListener rl)
      throws PageException {
    PageContextImpl pci = (PageContextImpl) pc;
    if (appPS != null) {
      String callPath = appPS.getComponentName();

      ComponentAccess app = ComponentLoader.loadComponent(pci, null, appPS, callPath, false, false);

      // init
      initApplicationContext(pci, app);

      apps.put(pc.getApplicationContext().getName(), app);

      if (!pci.initApplicationContext()) return;

      if (rl != null) {
        requestedPage = rl.execute(pc, requestedPage);
        if (requestedPage == null) return;
      }

      String targetPage = requestedPage.getFullRealpath();
      boolean doOnRequestEnd = true;

      // onRequestStart
      if (app.contains(pc, ON_REQUEST_START)) {
        Object rtn = call(app, pci, ON_REQUEST_START, new Object[] {targetPage}, true);
        if (!Caster.toBooleanValue(rtn, true)) return;
      }

      // onRequest
      boolean isCFC =
          ResourceUtil.getExtension(targetPage, "")
              .equalsIgnoreCase(pc.getConfig().getCFCExtension());
      Object method;
      if (isCFC
          && app.contains(pc, ON_CFCREQUEST)
          && (method = pc.urlFormScope().get(ComponentPage.METHOD, null)) != null) {

        Struct url = (Struct) Duplicator.duplicate(pc.urlFormScope(), true);

        url.removeEL(KeyConstants._fieldnames);
        url.removeEL(ComponentPage.METHOD);
        Object args = url.get(KeyConstants._argumentCollection, null);
        Object returnFormat = url.removeEL(KeyConstants._returnFormat);
        Object queryFormat = url.removeEL(KeyConstants._queryFormat);

        if (args == null) {
          args = pc.getHttpServletRequest().getAttribute("argumentCollection");
        }

        if (args instanceof String) {
          args = new JSONExpressionInterpreter().interpret(pc, (String) args);
        }

        if (args != null) {
          if (Decision.isCastableToStruct(args)) {
            Struct sct = Caster.toStruct(args, false);
            // Key[] keys = url.keys();
            Iterator<Entry<Key, Object>> it = url.entryIterator();
            Entry<Key, Object> e;
            while (it.hasNext()) {
              e = it.next();
              sct.setEL(e.getKey(), e.getValue());
            }
            args = sct;
          } else if (Decision.isCastableToArray(args)) {
            args = Caster.toArray(args);
          } else {
            Array arr = new ArrayImpl();
            arr.appendEL(args);
            args = arr;
          }
        } else args = url;

        Object rtn =
            call(
                app,
                pci,
                ON_CFCREQUEST,
                new Object[] {requestedPage.getComponentName(), method, args},
                true);

        if (rtn != null) {
          if (pc.getHttpServletRequest().getHeader("AMF-Forward") != null) {
            pc.variablesScope().setEL("AMF-Forward", rtn);
            // ThreadLocalWDDXResult.set(rtn);
          } else {
            try {
              pc.forceWrite(
                  ComponentPage.convertResult(
                      pc, app, method.toString(), returnFormat, queryFormat, rtn));
            } catch (Exception e) {
              throw Caster.toPageException(e);
            }
          }
        }

      }
      // else if(!isCFC && app.contains(pc,ON_REQUEST)) {}
      else {
        // TODO impl die nicht so generisch ist
        try {

          if (!isCFC && app.contains(pc, ON_REQUEST))
            call(app, pci, ON_REQUEST, new Object[] {targetPage}, false);
          else pci.doInclude(requestedPage);
        } catch (PageException pe) {
          if (!Abort.isSilentAbort(pe)) {
            if (pe instanceof MissingIncludeException) {
              if (((MissingIncludeException) pe).getPageSource().equals(requestedPage)) {
                if (app.contains(pc, ON_MISSING_TEMPLATE)) {
                  if (!Caster.toBooleanValue(
                      call(app, pci, ON_MISSING_TEMPLATE, new Object[] {targetPage}, true), true))
                    throw pe;
                } else throw pe;
              } else throw pe;
            } else throw pe;
          } else {
            doOnRequestEnd = false;
            if (app.contains(pc, ON_ABORT)) {
              call(app, pci, ON_ABORT, new Object[] {targetPage}, true);
            }
          }
        }
      }

      // onRequestEnd
      if (doOnRequestEnd && app.contains(pc, ON_REQUEST_END)) {
        call(app, pci, ON_REQUEST_END, new Object[] {targetPage}, true);
      }
    } else {
      apps.put(pc.getApplicationContext().getName(), null);
      pc.doInclude(requestedPage);
    }
  }
예제 #11
0
 @Override
 public boolean hasOnSessionStart(PageContext pc) {
   return hasOnSessionStart(pc, apps.get(pc.getApplicationContext().getName()));
 }
예제 #12
0
  private synchronized SessionFactory getSessionFactory(PageContext pc, boolean init)
      throws PageException {
    ApplicationContextPro appContext = ((ApplicationContextPro) pc.getApplicationContext());
    if (!appContext.isORMEnabled())
      throw new ORMException(this, "ORM is not enabled in application.cfc/cfapplication");

    this.hash = hash(appContext);

    // datasource
    String dsn = appContext.getORMDatasource();
    if (StringUtil.isEmpty(dsn))
      throw new ORMException(this, "missing datasource defintion in application.cfc/cfapplication");
    if (!dsn.equalsIgnoreCase(datasource)) {
      configuration = null;
      if (_factory != null) _factory.close();
      _factory = null;
      datasource = dsn.toLowerCase();
    }

    // config
    ormConf = appContext.getORMConfiguration();

    // List<Component> arr = null;
    arr = null;
    if (init) {
      if (ormConf.autogenmap()) {
        arr = HibernateSessionFactory.loadComponents(pc, this, ormConf);
        cfcs.clear();
      } else
        throw new HibernateException(this, "orm setting autogenmap=false is not supported yet");
    }

    // load entities
    if (!ArrayUtil.isEmpty(arr)) {
      loadNamingStrategy(ormConf);

      DatasourceConnectionPool pool =
          ((ConfigWebImpl) pc.getConfig()).getDatasourceConnectionPool();
      DatasourceConnection dc =
          pool.getDatasourceConnection(pc, pc.getConfig().getDataSource(dsn), null, null);
      // DataSourceManager manager = pc.getDataSourceManager();
      // DatasourceConnection dc=manager.getConnection(pc,dsn, null, null);
      this.ds = dc.getDatasource();
      try {
        Iterator<ComponentPro> it = arr.iterator();
        while (it.hasNext()) {
          createMapping(pc, it.next(), dc, ormConf);
        }
      } finally {
        pool.releaseDatasourceConnection(dc);
        // manager.releaseConnection(pc,dc);
      }
      if (arr.size() != cfcs.size()) {
        ComponentPro cfc;
        String name, lcName;
        Map<String, String> names = new HashMap<String, String>();
        Iterator<ComponentPro> it = arr.iterator();
        while (it.hasNext()) {
          cfc = it.next();
          name = HibernateCaster.getEntityName(cfc);
          lcName = name.toLowerCase();
          if (names.containsKey(lcName))
            throw new ORMException(
                this,
                "Entity Name ["
                    + name
                    + "] is ambigous, ["
                    + names.get(lcName)
                    + "] and ["
                    + cfc.getPageSource().getDisplayPath()
                    + "] use the same entity name.");
          names.put(lcName, cfc.getPageSource().getDisplayPath());
        }
      }
    }
    arr = null;
    if (configuration != null) return _factory;

    // MUST
    // cacheconfig
    // cacheprovider
    // ...

    String mappings = HibernateSessionFactory.createMappings(this, cfcs);

    DatasourceConnectionPool pool = ((ConfigWebImpl) pc.getConfig()).getDatasourceConnectionPool();
    DatasourceConnection dc =
        pool.getDatasourceConnection(pc, pc.getConfig().getDataSource(dsn), null, null);
    try {
      configuration = HibernateSessionFactory.createConfiguration(this, mappings, dc, ormConf);
    } catch (Exception e) {
      throw Caster.toPageException(e);
    } finally {
      pool.releaseDatasourceConnection(dc);
    }

    addEventListeners(pc, configuration, ormConf, cfcs);

    EntityTuplizerFactory tuplizerFactory = configuration.getEntityTuplizerFactory();
    // tuplizerFactory.registerDefaultTuplizerClass(EntityMode.MAP, CFCEntityTuplizer.class);
    // tuplizerFactory.registerDefaultTuplizerClass(EntityMode.MAP, MapEntityTuplizer.class);
    tuplizerFactory.registerDefaultTuplizerClass(EntityMode.MAP, AbstractEntityTuplizerImpl.class);
    tuplizerFactory.registerDefaultTuplizerClass(EntityMode.POJO, AbstractEntityTuplizerImpl.class);
    // tuplizerFactory.registerDefaultTuplizerClass(EntityMode.POJO,
    // AbstractEntityTuplizerImpl.class);

    // configuration.setEntityResolver(new CFCEntityResolver());
    // configuration.setEntityNotFoundDelegate(new EntityNotFoundDelegate());

    return _factory = configuration.buildSessionFactory();
  }