Search in sources :

Example 1 with Container

use of alma.acs.tmcdb.Container in project ACS by ACS-Community.

the class ContainerStartupOptionHelper method convertContainerStartupOptions.

/**
	 * Converts a list of ContainerStartupOption to a flat option string
	 * that can be passed to the container daemon or used to satisfy DAL calls.
	 * @param options
	 * @return  Options in one string, wrapped as needed. Possibly empty string, never null.
	 * @throws IllegalArgumentException if an option references a different container than the other options 
	 *                                  (all refs null is OK though)
	 */
public String convertContainerStartupOptions(Collection<ContainerStartupOption> options) {
    if (options == null || options.isEmpty()) {
        logger.finer("convertContainerStartupOptions called without options.");
        return "";
    }
    String execArgs = "";
    String execArgsLang = "";
    String contArgs = "";
    Container commonContainer = null;
    for (ContainerStartupOption option : options) {
        // validate container ref
        if (commonContainer == null) {
            commonContainer = option.getContainer();
        } else if (option.getContainer() != commonContainer) {
            throw new IllegalArgumentException();
        }
        // gather by option type
        if (option.getOptionType().equals(ContStartOptType.ENV_VAR)) {
            logger.warning("Ignoring option of type " + ContStartOptType.ENV_VAR);
        } else if (option.getOptionType().equals(ContStartOptType.EXEC_ARG)) {
            execArgs += " " + option.getOptionValue();
        } else if (option.getOptionType().equals(ContStartOptType.EXEC_ARG_LANG)) {
            execArgsLang += " " + option.getOptionValue();
        } else if (option.getOptionType().equals(ContStartOptType.CONT_ARG)) {
            contArgs += " " + option.getOptionValue();
        }
    }
    // wrap and concatenate the options
    String ret = execArgs.trim();
    if (execArgsLang.length() > 0) {
        ret += " " + EXEC_ARG_LANG_WRAPPER_OPTION + "=\"" + execArgsLang.trim() + "\"";
    }
    if (contArgs.length() > 0) {
        ret += " " + CONT_ARG_WRAPPER_OPTION + "=\"" + contArgs.trim() + "\"";
    }
    if (commonContainer != null && logger.isLoggable(Level.FINER)) {
        logger.finer(options.size() + " options for container " + commonContainer + " flattened: " + ret.trim());
    }
    return ret.trim();
}
Also used : ContainerStartupOption(alma.acs.tmcdb.ContainerStartupOption) Container(alma.acs.tmcdb.Container)

Example 2 with Container

use of alma.acs.tmcdb.Container in project ACS by ACS-Community.

the class HibernateWDALImpl method getContainersTableMap.

public Map<String, Object> getContainersTableMap() {
    final String keyField = "Name";
    final Class type = alma.TMCDB.maci.Container.class;
    Map<String, Object> map = new RootMap<String, Object>();
    try {
        Session session = hibernateUtil.getSession();
        Method accessor = DOMJavaClassIntrospector.getAccessorMethod(type, keyField);
        Field field = null;
        if (accessor == null) {
            try {
                field = type.getField(keyField);
            } catch (Throwable th) {
                throw new IllegalStateException("failed to obtain key ");
            }
        }
        List list = getListForConfiguration(session, type);
        // Sort the list by path + component to ensure that parent containers are added before their children
        Comparator<alma.TMCDB.maci.Container> comparator = new Comparator<alma.TMCDB.maci.Container>() {

            public int compare(alma.TMCDB.maci.Container o1, alma.TMCDB.maci.Container o2) {
                String fullName1 = ((o1.Path == null ? "" : o1.Path) + "/") + o1.getName();
                String fullName2 = ((o2.Path == null ? "" : o2.Path) + "/") + o2.getName();
                return fullName1.compareTo(fullName2);
            }
        };
        Collections.sort(list, comparator);
        for (Object data : list) {
            String baseKey;
            if (accessor != null)
                baseKey = accessor.invoke(data, (Object[]) null).toString();
            else
                //if (field != null)
                baseKey = field.get(data).toString();
            // baseKey should not be null
            Map parentMap = map;
            alma.TMCDB.maci.Container container = (alma.TMCDB.maci.Container) data;
            // do not include this
            if (DUMMY_CONTAINER_FLAG.equals(container.getDeployInfo().getTypeModifiers()))
                continue;
            // some cleaning
            if (container.getDeployInfo() != null && container.getDeployInfo().getHost() == null)
                container.setDeployInfo(null);
            // now find its map
            String path = getNormalizedPath(container.Path);
            while (path != null && path.length() > 0) {
                // remove trailing slashes, to have unique curl (used for key)
                if (path.charAt(0) == '/') {
                    path = path.substring(1);
                    continue;
                }
                int pos = path.indexOf('/');
                String parentPath = (pos > 0) ? path.substring(0, pos) : path;
                String subpath = (pos > 0) ? path.substring(pos + 1, path.length()) : null;
                alma.TMCDB.maci.ContainerNode parentContainer = (alma.TMCDB.maci.ContainerNode) parentMap.get(parentPath);
                if (parentContainer == null) {
                    parentContainer = new alma.TMCDB.maci.ContainerNode();
                    parentMap.put(parentPath, parentContainer);
                }
                parentMap = parentContainer._;
                path = subpath;
            }
            // unique key generation
            int count = 0;
            String key = baseKey;
            while (parentMap.containsKey(key)) key = baseKey + String.valueOf(++count);
            parentMap.put(key, data);
            if (data instanceof alma.TMCDB.maci.Container) {
                alma.TMCDB.maci.Container cont = (alma.TMCDB.maci.Container) data;
                m_logger.finer("Loaded container name=" + cont.Path + cont.getName() + ", implLang=" + cont.getImplLang());
            } else {
                m_logger.warning("Bad container class '" + data.getClass().getName() + "' read from TMCDB.");
            }
        }
    } catch (Throwable th) {
        th.printStackTrace();
    }
    return map;
}
Also used : RootMap(com.cosylab.cdb.jdal.hibernate.RootMap) Method(java.lang.reflect.Method) Comparator(java.util.Comparator) Field(java.lang.reflect.Field) Container(alma.acs.tmcdb.Container) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Map(java.util.Map) RootMap(com.cosylab.cdb.jdal.hibernate.RootMap) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Session(org.hibernate.Session)

Example 3 with Container

use of alma.acs.tmcdb.Container in project ACS by ACS-Community.

the class ContainerStartupOptionHelperTest method testFromOptionsString.

/**
	 * Tests parsing of an options string into the 3 categories.
	 */
public void testFromOptionsString() {
    Container container = new Container();
    logger.info("Will convert the following flags string: " + testFlags);
    Collection<ContainerStartupOption> options = optionHelper.convertFlagsString(container, testFlags);
    assertEquals("Expected 3 ContainerStartupOption instances, 2 for the wrapper contents and 1 for the unwrapped options.", 3, options.size());
    for (ContainerStartupOption option : options) {
        assertSame("container reference should be as provided", container, option.getContainer());
        assertEquals(ContainerStartupOptionHelper.OPTION_NAME_LEGACY_CONCATENATED, option.getOptionName());
        if (option.getOptionType().equals(ContStartOptType.EXEC_ARG)) {
            assertEquals("all unwrapped options expected here.", "-e myOtherContainerExecutable --managerReference=corbalocToOtherManager", option.getOptionValue());
            logger.info("EXEC_ARG was OK");
        } else if (option.getOptionType().equals(ContStartOptType.EXEC_ARG_LANG)) {
            assertEquals("--passthroughProcessStart options expected here.", "-maxHeapSize 100m -clientVM -D mytest.prop=dontdoit", option.getOptionValue());
            logger.info("EXEC_ARG_LANG was OK");
        } else if (option.getOptionType().equals(ContStartOptType.CONT_ARG)) {
            assertEquals("--passthrough options expected here.", "-myDummyContainerArg 77", option.getOptionValue());
            logger.info("CONT_ARG was OK");
        }
    }
}
Also used : ContainerStartupOption(alma.acs.tmcdb.ContainerStartupOption) Container(alma.acs.tmcdb.Container)

Example 4 with Container

use of alma.acs.tmcdb.Container in project ACS by ACS-Community.

the class ContainerStartupOptionHelperTest method testToOptionsString.

public void testToOptionsString() {
    Container container = new Container();
    ContainerStartupOption execOpt1 = new ContainerStartupOptionForTest(container, ContStartOptType.EXEC_ARG, "-e myOtherContainerExecutable");
    ContainerStartupOption execOpt2 = new ContainerStartupOptionForTest(container, ContStartOptType.EXEC_ARG, "--managerReference=corbalocToOtherManager");
    ContainerStartupOption execLangOpt1 = new ContainerStartupOptionForTest(container, ContStartOptType.EXEC_ARG_LANG, "-maxHeapSize 100m");
    // next option is not "atomic", but that should be tolerated
    ContainerStartupOption execLangOpt2 = new ContainerStartupOptionForTest(container, ContStartOptType.EXEC_ARG_LANG, "-clientVM -D mytest.prop=dontdoit");
    ContainerStartupOption contOpt1 = new ContainerStartupOptionForTest(container, ContStartOptType.CONT_ARG, "-myDummyContainerArg 77");
    // give options in mixed order with respect to type
    List<ContainerStartupOption> options = new ArrayList<ContainerStartupOption>();
    options.add(execOpt1);
    options.add(execLangOpt1);
    options.add(contOpt1);
    options.add(execLangOpt2);
    options.add(execOpt2);
    String flags = optionHelper.convertContainerStartupOptions(options);
    assertEquals("Converted ContainerStartupOptions should match the fixed test flags string", testFlags, flags);
}
Also used : ContainerStartupOption(alma.acs.tmcdb.ContainerStartupOption) Container(alma.acs.tmcdb.Container) ArrayList(java.util.ArrayList)

Example 5 with Container

use of alma.acs.tmcdb.Container in project ACS by ACS-Community.

the class HibernateWDALImpl method updateContainersTableMap.

public void updateContainersTableMap(String curl) {
    m_logger.info("clear_cache(curl): ContainersTable1");
    final String keyField = "Name";
    final Class type = alma.TMCDB.maci.Container.class;
    Map<String, Object> rootMap = (Map<String, Object>) rootNode;
    Map<String, Object> map = (Map<String, Object>) ((Map<String, Object>) rootMap.get("MACI")).get("Containers");
    m_logger.info("clear_cache(curl): ContainersTable2");
    try {
        Session session = hibernateUtil.getSession();
        Method accessor = DOMJavaClassIntrospector.getAccessorMethod(type, keyField);
        Field field = null;
        if (accessor == null) {
            try {
                field = type.getField(keyField);
            } catch (Throwable th) {
                throw new IllegalStateException("failed to obtain key ");
            }
        }
        m_logger.info("clear_cache(curl): ContainersTable3");
        String[] els = curl.split("/");
        String rpath = "^/*";
        String rsubpath = "^/*";
        String rcpath = "^/*";
        String rcname = els[els.length - 1];
        for (int i = 0; i < els.length; i++) {
            rpath += els[i];
            rsubpath += els[i];
            if (i < els.length - 1) {
                rpath += "/+";
                rsubpath += "/+";
                rcpath += els[i];
                if (i < els.length - 2)
                    rcpath += "/+";
            }
        }
        rpath += "/*$";
        rsubpath += "/+.*";
        rcpath += "/*$";
        System.out.println(rpath);
        System.out.println(rsubpath);
        System.out.println(rcpath + "|" + rcname);
        m_logger.info("clear_cache(curl): ContainersTable4");
        //Consider the cases where the curl matches exactly the Path, where
        //it is part of the path and when it matches exactly the path and
        //the component name.
        Criterion cr = Restrictions.disjunction().add(getRegularExpressionRestriction("Path", rpath)).add(getRegularExpressionRestriction("Path", rsubpath)).add(Restrictions.and(getRegularExpressionRestriction("Path", rcpath), Restrictions.eq("Name", rcname)));
        m_logger.info("clear_cache(curl): ContainersTable5");
        List list = getListForConfiguration(session, type, cr);
        m_logger.info("clear_cache(curl): ContainersTable6");
        System.out.println("\nFound the following Containers");
        for (Object data : list) {
            System.out.println(((alma.TMCDB.maci.Container) data).Path + "/" + ((alma.TMCDB.maci.Container) data).getName());
        }
        //Remove the entries from existing maps.
        System.out.println("\nChecking maps to remove");
        m_logger.info("clear_cache(curl): ContainersTable7");
        Map rParentMap = map;
        for (int i = 0; i < els.length; i++) {
            System.out.println("Checking path " + els[i] + ".");
            System.out.println("Parent keys: " + rParentMap.keySet().toString());
            Object data = rParentMap.get(els[i]);
            if (data == null) {
                System.out.println("No element found with the given curl");
                break;
            } else {
                if (data instanceof alma.TMCDB.maci.Container) {
                    System.out.println("Instance of Container (Container!).");
                } else if (data instanceof alma.TMCDB.maci.ContainerNode) {
                    System.out.println("Instance of ContainerNode (Path!).");
                } else {
                    System.out.println("Unknown type! Details: " + data.toString());
                }
                if (i < els.length - 1) {
                    System.out.println("There are elements remaining, so we proceed to next element in path.");
                    rParentMap = ((alma.TMCDB.maci.ContainerNode) data)._;
                } else {
                    System.out.println("There are no elements remaining, we remove all entries from this element in path and on.");
                    rParentMap.remove(els[i]);
                }
            }
        }
        m_logger.info("clear_cache(curl): ContainersTable8");
        // Sort the list by path + component to ensure that parent containers are added before their children
        Comparator<alma.TMCDB.maci.Container> comparator = new Comparator<alma.TMCDB.maci.Container>() {

            public int compare(alma.TMCDB.maci.Container o1, alma.TMCDB.maci.Container o2) {
                String fullName1 = ((o1.Path == null ? "" : o1.Path) + "/") + o1.getName();
                String fullName2 = ((o2.Path == null ? "" : o2.Path) + "/") + o2.getName();
                return fullName1.compareTo(fullName2);
            }
        };
        Collections.sort(list, comparator);
        m_logger.info("clear_cache(curl): ContainersTable9");
        for (Object data : list) {
            String baseKey;
            if (accessor != null)
                baseKey = accessor.invoke(data, (Object[]) null).toString();
            else
                //if (field != null)
                baseKey = field.get(data).toString();
            // baseKey should not be null
            Map parentMap = map;
            alma.TMCDB.maci.Container container = (alma.TMCDB.maci.Container) data;
            // do not include this
            if (DUMMY_CONTAINER_FLAG.equals(container.getDeployInfo().getTypeModifiers()))
                continue;
            // some cleaning
            if (container.getDeployInfo() != null && container.getDeployInfo().getHost() == null)
                container.setDeployInfo(null);
            // now find its map
            String path = getNormalizedPath(container.Path);
            while (path != null && path.length() > 0) {
                // remove trailing slashes, to have unique curl (used for key)
                if (path.charAt(0) == '/') {
                    path = path.substring(1);
                    continue;
                }
                int pos = path.indexOf('/');
                String parentPath = (pos > 0) ? path.substring(0, pos) : path;
                String subpath = (pos > 0) ? path.substring(pos + 1, path.length()) : null;
                alma.TMCDB.maci.ContainerNode parentContainer = (alma.TMCDB.maci.ContainerNode) parentMap.get(parentPath);
                if (parentContainer == null) {
                    parentContainer = new alma.TMCDB.maci.ContainerNode();
                    parentMap.put(parentPath, parentContainer);
                }
                parentMap = parentContainer._;
                path = subpath;
            }
            // unique key generation
            int count = 0;
            String key = baseKey;
            while (parentMap.containsKey(key)) key = baseKey + String.valueOf(++count);
            parentMap.put(key, data);
            if (data instanceof alma.TMCDB.maci.Container) {
                alma.TMCDB.maci.Container cont = (alma.TMCDB.maci.Container) data;
                m_logger.finer("Loaded container name=" + cont.Path + cont.getName() + ", implLang=" + cont.getImplLang());
            } else {
                m_logger.warning("Bad container class '" + data.getClass().getName() + "' read from TMCDB.");
            }
            m_logger.info("clear_cache(curl): ContainersTable10");
        }
        m_logger.info("clear_cache(curl): ContainersTable11");
    } catch (Throwable th) {
        th.printStackTrace();
    }
    m_logger.info("clear_cache(curl): ContainersTable12");
}
Also used : Method(java.lang.reflect.Method) Comparator(java.util.Comparator) Field(java.lang.reflect.Field) Container(alma.acs.tmcdb.Container) Criterion(org.hibernate.criterion.Criterion) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Map(java.util.Map) RootMap(com.cosylab.cdb.jdal.hibernate.RootMap) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Session(org.hibernate.Session)

Aggregations

Container (alma.acs.tmcdb.Container)6 ContainerStartupOption (alma.acs.tmcdb.ContainerStartupOption)4 ArrayList (java.util.ArrayList)3 RootMap (com.cosylab.cdb.jdal.hibernate.RootMap)2 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 Comparator (java.util.Comparator)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 Session (org.hibernate.Session)2 BACIPropertyType (alma.TMCDB.baci.BACIPropertyType)1 EmptyStringHandlerBACIPropertyType (alma.TMCDB.baci.EmptyStringHandlerBACIPropertyType)1 ClientLogManager (alma.acs.logging.ClientLogManager)1 BACIProperty (alma.acs.tmcdb.BACIProperty)1 ChannelMapping (alma.acs.tmcdb.ChannelMapping)1 Component (alma.acs.tmcdb.Component)1 ComponentType (alma.acs.tmcdb.ComponentType)1