Package org.objectweb.speedo.naming.lib

Source Code of org.objectweb.speedo.naming.lib.NamingManagerHelper

/**
* Copyright (C) 2001-2004 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
package org.objectweb.speedo.naming.lib;

import java.util.ArrayList;
import java.util.Map;
import java.util.StringTokenizer;

import org.objectweb.jorm.api.PClassMapping;
import org.objectweb.jorm.api.PException;
import org.objectweb.jorm.api.PMapper;
import org.objectweb.jorm.metainfo.api.Manager;
import org.objectweb.jorm.naming.api.PBinder;
import org.objectweb.jorm.naming.api.PNamingContext;
import org.objectweb.jorm.naming.api.PolymorphicPNamingContext;
import org.objectweb.perseus.cache.api.CacheManager;
import org.objectweb.speedo.api.SpeedoException;
import org.objectweb.speedo.metadata.SpeedoClass;
import org.objectweb.speedo.metadata.SpeedoColumn;
import org.objectweb.speedo.naming.api.NamingManager;
import org.objectweb.speedo.pm.api.POManagerFactoryItf;
import org.objectweb.util.monolog.api.Logger;

/**
*
* @author S.Chassande-Barrioz
*/
public abstract class NamingManagerHelper implements NamingManager {

  public final static String HINTS_SEP = ",";

  public final static int ID_CAT_IDX = 0;
  public final static int BINDER_IDX = 1;
  public final static int PNC_IDX = 2;
  public final static int PCLASS_IDX = 3;

  public final static String POLYMORPHIC_PNC = "org.objectweb.jorm.naming.lib.BasicPolymorphicPNamingContext";
 
  protected Logger logger;
  protected CacheManager cache;
  protected POManagerFactoryItf pmf;
  protected PMapper mapper;

  public boolean supportPNamingcontext() {
    return true;
  }

  public void setPMapper(PMapper mapper) {
      this.mapper = mapper;
  }

  public void setCache(CacheManager cache) {
    this.cache = cache;
  }
 
  public void setLogger(Logger logger) {
    this.logger = logger;
  }

  public void setPmf(POManagerFactoryItf pmf) {
    this.pmf = pmf;
  }
 
  public NamingManager.NamingField[] getNamingfields(SpeedoClass sc) throws PException {
    return null;
  }

  protected abstract String getName();

  public boolean canProvidePBinder(Object hints, ClassLoader classLoader) {
    return hints instanceof String
      && ((String) hints).startsWith(getName() + HINTS_SEP);
  }

  public boolean canProvidePNamingContext(Object hints, ClassLoader classLoader) {
    return hints instanceof String
      && ((String) hints).startsWith(getName() + HINTS_SEP);
  }

  public PBinder getPBinder(String className,
                String hints,
                ClassLoader classLoader,
                byte mappingStructureRule,
                Map cn2binder,
                Map cn2pnc) throws PException {

    try {
      return (PBinder) classLoader.loadClass(
        getBinderClassNameFromHints(hints, getName())).newInstance();
    } catch (Exception e) {
      throw new PException(e,
          "Impossible to instanciate the binder of the class "
          + hints);
    }
  }

  public PNamingContext getPNamingContext(String className,
                      String hints,
                      ClassLoader classLoader,
                      byte mappingStructureRule,
                      Map cn2binder,
                      Map cn2pnc,
                      Manager miManager,
                      PClassMapping pcm) throws PException {
    String[] tokens = getTokens(hints);
    PNamingContext pnc = null;
    if (tokens[BINDER_IDX].equals(tokens[PNC_IDX])) {
      //The binder must be used as PNamingContext
      pnc = (PNamingContext) cn2binder.get(className);
      if (pnc == null) {
        //instanciate the binder/PNC
        pnc = (PNamingContext) getPBinder(className, hints, classLoader,
          mappingStructureRule, cn2binder, cn2pnc);
        //register the binder/PNC as binder
        cn2binder.put(className, pnc);
      }
      return pnc;
    } else {
      boolean register = false;
      if (!tokens[PCLASS_IDX].equals(className) && !tokens[PNC_IDX].equals(POLYMORPHIC_PNC)) {
        //The PNC of THE ancestor must be used
        pnc = (PNamingContext) cn2pnc.get(tokens[PCLASS_IDX]);
        if (pnc == null) {
          register = true;
        }
      }
      if (pnc == null) {
        //instanciate the PNC
        try {
          pnc = (PNamingContext) classLoader
            .loadClass(tokens[PNC_IDX]).newInstance();
          //if the PNC is polymorphic
          if (tokens[PNC_IDX].equals(POLYMORPHIC_PNC)) {
            PolymorphicPNamingContext polymorphicPNC = (PolymorphicPNamingContext) pnc;
            //link the cache
            //cache null
            polymorphicPNC.setCache(this.cache);
            //instanciate a new binder
            PBinder delegatedBinder = getPBinder(className, hints, classLoader,
                mappingStructureRule, cn2binder, cn2pnc);
            //link a new instanciated and dedicated binder
            polymorphicPNC.setDelegatedBinder(delegatedBinder)
            delegatedBinder.setPClassMapping(pcm);
            delegatedBinder.setPType(pcm.getPType());
            //link the PClassMapping
            polymorphicPNC.setPcm(delegatedBinder.getBinderClassMapping());
          }
        } catch (Exception e) {
          throw new PException(e,
            "Impossible to instanciate the PNC of the class "
            + hints);
        }
        if (register) {
          cn2pnc.put(tokens[PCLASS_IDX], pnc);
        }
      }
    }
    return pnc;
  }

    public SpeedoColumn[] getDefaultColumn(SpeedoClass sc) {
        return null;
    }
 
  public static String[] getTokens(Object o) {
    if (!(o instanceof String)) {
      return null;
    }
    StringTokenizer st = new StringTokenizer((String) o, HINTS_SEP, true);
    ArrayList tokens = new ArrayList();
    boolean previousIsSep = false;
    while (st.hasMoreTokens()) {
      String token = st.nextToken();
      if (HINTS_SEP.equals(token)) {
        if (previousIsSep) {
          tokens.add("");
        }
        previousIsSep = true;
      } else {
        tokens.add(token);
        previousIsSep = false;
      }
    }
    tokens.add("");
    return (String[]) tokens.toArray(new String[tokens.size()]);
  }

  public static String getBinderClassNameFromHints(Object hints, String idCatName) {
    String[] tokens = getTokens(hints);
    if (tokens == null || tokens.length < 4
      || !tokens[0].equals(idCatName)) {
      return null;
    } else {
      return tokens[BINDER_IDX];
    }
  }

  public static String getPNCClassNameFromHints(Object hints, String idCatName) {
    String[] tokens = getTokens(hints);
    if (tokens == null || tokens.length < 4
      || !tokens[0].equals(idCatName)) {
      return null;
    } else {
      return tokens[PNC_IDX];
    }
  }
 
    public boolean needInheritanceDiscriminator(SpeedoClass sc)
            throws SpeedoException {
        return false;
    }
}
TOP

Related Classes of org.objectweb.speedo.naming.lib.NamingManagerHelper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.