Search in sources :

Example 6 with MappingImpl

use of lucee.runtime.MappingImpl in project Lucee by lucee.

the class AppListenerUtil method toMappings.

private static Mapping[] toMappings(ConfigWeb cw, String type, Object o, boolean useStructNames, Resource source) throws PageException {
    ConfigWebImpl config = (ConfigWebImpl) cw;
    Array array;
    if (o instanceof String) {
        array = ListUtil.listToArrayRemoveEmpty(Caster.toString(o), ',');
    } else if (o instanceof Struct) {
        Struct sct = (Struct) o;
        if (useStructNames) {
            Iterator<Entry<Key, Object>> it = sct.entryIterator();
            List<Mapping> list = new ArrayList<Mapping>();
            Entry<Key, Object> e;
            String virtual;
            while (it.hasNext()) {
                e = it.next();
                virtual = e.getKey().getString();
                if (virtual.length() == 0)
                    virtual = "/";
                if (!virtual.startsWith("/"))
                    virtual = "/" + virtual;
                if (!virtual.equals("/") && virtual.endsWith("/"))
                    virtual = virtual.substring(0, virtual.length() - 1);
                MappingData md = toMappingData(e.getValue(), source);
                list.add(config.getApplicationMapping(type, virtual, md.physical, md.archive, md.physicalFirst, true));
            }
            return list.toArray(new Mapping[list.size()]);
        }
        array = new ArrayImpl();
        Iterator<Object> it = sct.valueIterator();
        while (it.hasNext()) {
            array.append(it.next());
        }
    } else {
        array = Caster.toArray(o);
    }
    MappingImpl[] mappings = new MappingImpl[array.size()];
    for (int i = 0; i < mappings.length; i++) {
        MappingData md = toMappingData(array.getE(i + 1), source);
        mappings[i] = (MappingImpl) config.getApplicationMapping(type, "/" + i, md.physical, md.archive, md.physicalFirst, true);
    }
    return mappings;
}
Also used : ArrayImpl(lucee.runtime.type.ArrayImpl) Mapping(lucee.runtime.Mapping) MappingImpl(lucee.runtime.MappingImpl) Struct(lucee.runtime.type.Struct) Array(lucee.runtime.type.Array) ConfigWebImpl(lucee.runtime.config.ConfigWebImpl) Entry(java.util.Map.Entry) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) Key(lucee.runtime.type.Collection.Key)

Example 7 with MappingImpl

use of lucee.runtime.MappingImpl in project Lucee by lucee.

the class Admin method doGetComponentMappings.

private void doGetComponentMappings() throws PageException {
    Mapping[] mappings = config.getComponentMappings();
    lucee.runtime.type.Query qry = new QueryImpl(new String[] { "archive", "strarchive", "physical", "strphysical", "virtual", "hidden", "physicalFirst", "readonly", "inspect" }, mappings.length, "query");
    for (int i = 0; i < mappings.length; i++) {
        MappingImpl m = (MappingImpl) mappings[i];
        int row = i + 1;
        qry.setAt("archive", row, m.getArchive());
        qry.setAt("strarchive", row, m.getStrArchive());
        qry.setAt("physical", row, m.getPhysical());
        qry.setAt("strphysical", row, m.getStrPhysical());
        qry.setAt("virtual", row, m.getVirtual());
        qry.setAt("hidden", row, Caster.toBoolean(m.isHidden()));
        qry.setAt("physicalFirst", row, Caster.toBoolean(m.isPhysicalFirst()));
        qry.setAt("readonly", row, Caster.toBoolean(m.isReadonly()));
        qry.setAt("inspect", row, ConfigWebUtil.inspectTemplate(m.getInspectTemplateRaw(), ""));
    }
    pageContext.setVariable(getString("admin", action, "returnVariable"), qry);
}
Also used : QueryImpl(lucee.runtime.type.QueryImpl) Mapping(lucee.runtime.Mapping) MappingImpl(lucee.runtime.MappingImpl) Query(lucee.runtime.type.Query)

Example 8 with MappingImpl

use of lucee.runtime.MappingImpl in project Lucee by lucee.

the class Admin method doGetMappings.

private void doGetMappings() throws PageException {
    Mapping[] mappings = config.getMappings();
    lucee.runtime.type.Query qry = new QueryImpl(new String[] { "archive", "strarchive", "physical", "strphysical", "virtual", "hidden", "physicalFirst", "readonly", "inspect", "toplevel" }, mappings.length, "query");
    for (int i = 0; i < mappings.length; i++) {
        MappingImpl m = (MappingImpl) mappings[i];
        int row = i + 1;
        qry.setAt("archive", row, m.getArchive());
        qry.setAt("strarchive", row, m.getStrArchive());
        qry.setAt("physical", row, m.getPhysical());
        qry.setAt("strphysical", row, m.getStrPhysical());
        qry.setAt("virtual", row, m.getVirtual());
        qry.setAt("hidden", row, Caster.toBoolean(m.isHidden()));
        qry.setAt("physicalFirst", row, Caster.toBoolean(m.isPhysicalFirst()));
        qry.setAt("readonly", row, Caster.toBoolean(m.isReadonly()));
        qry.setAt("inspect", row, ConfigWebUtil.inspectTemplate(m.getInspectTemplateRaw(), ""));
        qry.setAt("toplevel", row, Caster.toBoolean(m.isTopLevel()));
    }
    pageContext.setVariable(getString("admin", action, "returnVariable"), qry);
}
Also used : QueryImpl(lucee.runtime.type.QueryImpl) Mapping(lucee.runtime.Mapping) MappingImpl(lucee.runtime.MappingImpl) Query(lucee.runtime.type.Query)

Example 9 with MappingImpl

use of lucee.runtime.MappingImpl in project Lucee by lucee.

the class Admin method doCreateArchive.

private void doCreateArchive(short mappingType) throws PageException {
    String virtual = getString("admin", action, "virtual").toLowerCase();
    String strFile = getString("admin", action, "file");
    Resource file = ResourceUtil.toResourceNotExisting(pageContext, strFile);
    boolean addCFMLFiles = getBoolV("addCFMLFiles", true);
    boolean addNonCFMLFiles = getBoolV("addNonCFMLFiles", true);
    Boolean ignoreScopes = getBool("ignoreScopes", null);
    // compile
    MappingImpl mapping = (MappingImpl) doCompileMapping(mappingType, virtual, true, ignoreScopes);
    // class files
    if (mapping == null)
        throw new ApplicationException("there is no mapping for [" + virtual + "]");
    if (!mapping.hasPhysical())
        throw new ApplicationException("mapping [" + virtual + "] has no physical directory");
    Resource classRoot = mapping.getClassRootDirectory();
    Resource temp = SystemUtil.getTempDirectory().getRealResource("mani-" + IDGenerator.stringId());
    Resource mani = temp.getRealResource("META-INF/MANIFEST.MF");
    try {
        if (file.exists())
            file.delete();
        if (!file.exists())
            file.createFile(true);
        ResourceFilter filter;
        // include everything, no filter needed
        if (addCFMLFiles && addNonCFMLFiles)
            filter = null;
        else // CFML Files but no other files
        if (addCFMLFiles) {
            if (mappingType == MAPPING_CFC)
                filter = new ExtensionResourceFilter(ArrayUtil.toArray(Constants.getComponentExtensions(), "class", "MF"), true, true);
            else
                filter = new ExtensionResourceFilter(ArrayUtil.toArray(Constants.getExtensions(), "class", "MF"), true, true);
        } else // No CFML Files, but all other files
        if (addNonCFMLFiles) {
            filter = new NotResourceFilter(new ExtensionResourceFilter(Constants.getExtensions(), false, true));
        } else // no files at all
        {
            filter = new ExtensionResourceFilter(new String[] { "class", "MF" }, true, true);
        }
        String id = HashUtil.create64BitHashAsString(mapping.getStrPhysical(), Character.MAX_RADIX);
        // String id = MD5.getDigestAsString(mapping.getStrPhysical());
        String type;
        if (mappingType == MAPPING_CFC)
            type = "cfc";
        else if (mappingType == MAPPING_CT)
            type = "ct";
        else
            type = "regular";
        String token = HashUtil.create64BitHashAsString(System.currentTimeMillis() + "", Character.MAX_RADIX);
        // create manifest
        Manifest mf = new Manifest();
        // StringBuilder manifest=new StringBuilder();
        // Write OSGi specific stuff
        Attributes attrs = mf.getMainAttributes();
        attrs.putValue("Bundle-ManifestVersion", Caster.toString(BundleBuilderFactory.MANIFEST_VERSION));
        attrs.putValue("Bundle-SymbolicName", id);
        attrs.putValue("Bundle-Name", ListUtil.trim(mapping.getVirtual().replace('/', '.'), "."));
        attrs.putValue("Bundle-Description", "this is a " + type + " mapping generated by " + Constants.NAME + ".");
        attrs.putValue("Bundle-Version", "1.0.0." + token);
        // attrs.putValue("Import-Package","lucee.*");
        attrs.putValue("Require-Bundle", "lucee.core");
        // Mapping
        attrs.putValue("mapping-id", id);
        attrs.putValue("mapping-type", type);
        attrs.putValue("mapping-virtual-path", mapping.getVirtual());
        attrs.putValue("mapping-hidden", Caster.toString(mapping.isHidden()));
        attrs.putValue("mapping-physical-first", Caster.toString(mapping.isPhysicalFirst()));
        attrs.putValue("mapping-readonly", Caster.toString(mapping.isReadonly()));
        attrs.putValue("mapping-top-level", Caster.toString(mapping.isTopLevel()));
        attrs.putValue("mapping-inspect", ConfigWebUtil.inspectTemplate(mapping.getInspectTemplateRaw(), ""));
        attrs.putValue("mapping-listener-type", ConfigWebUtil.toListenerType(mapping.getListenerType(), ""));
        attrs.putValue("mapping-listener-mode", ConfigWebUtil.toListenerMode(mapping.getListenerMode(), ""));
        mani.createFile(true);
        IOUtil.write(mani, ManifestUtil.toString(mf, 100, null, null), "UTF-8", false);
        // source files
        Resource[] sources;
        if (!addCFMLFiles && !addNonCFMLFiles)
            sources = new Resource[] { temp, classRoot };
        else
            sources = new Resource[] { temp, mapping.getPhysical(), classRoot };
        CompressUtil.compressZip(ResourceUtil.listResources(sources, filter), file, filter);
        if (getBoolV("append", false)) {
            if (mappingType == MAPPING_CFC) {
                admin.updateComponentMapping(mapping.getVirtual(), mapping.getStrPhysical(), strFile, mapping.isPhysicalFirst() ? "physical" : "archive", mapping.getInspectTemplateRaw());
            } else if (mappingType == MAPPING_CT) {
                admin.updateCustomTag(mapping.getVirtual(), mapping.getStrPhysical(), strFile, mapping.isPhysicalFirst() ? "physical" : "archive", mapping.getInspectTemplateRaw());
            } else
                admin.updateMapping(mapping.getVirtual(), mapping.getStrPhysical(), strFile, mapping.isPhysicalFirst() ? "physical" : "archive", mapping.getInspectTemplateRaw(), mapping.isTopLevel(), mapping.getListenerMode(), mapping.getListenerType(), mapping.isReadonly());
            store();
        }
    } catch (IOException e) {
        throw Caster.toPageException(e);
    } finally {
        ResourceUtil.removeEL(temp, true);
    }
    adminSync.broadcast(attributes, config);
}
Also used : Resource(lucee.commons.io.res.Resource) DynamicAttributes(lucee.runtime.ext.tag.DynamicAttributes) Attributes(java.util.jar.Attributes) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) MappingImpl(lucee.runtime.MappingImpl) ExtensionResourceFilter(lucee.commons.io.res.filter.ExtensionResourceFilter) OrResourceFilter(lucee.commons.io.res.filter.OrResourceFilter) DirectoryResourceFilter(lucee.commons.io.res.filter.DirectoryResourceFilter) NotResourceFilter(lucee.commons.io.res.filter.NotResourceFilter) ResourceFilter(lucee.commons.io.res.filter.ResourceFilter) ApplicationException(lucee.runtime.exp.ApplicationException) NotResourceFilter(lucee.commons.io.res.filter.NotResourceFilter) ExtensionResourceFilter(lucee.commons.io.res.filter.ExtensionResourceFilter)

Example 10 with MappingImpl

use of lucee.runtime.MappingImpl in project Lucee by lucee.

the class XMLConfigWebFactory method loadMappings.

/**
 * load mapings from XML Document
 *
 * @param configServer
 * @param config
 * @param doc
 * @throws IOException
 */
private static void loadMappings(ConfigServerImpl configServer, ConfigImpl config, Document doc, int mode) throws IOException {
    boolean hasAccess = ConfigWebUtil.hasAccess(config, SecurityManager.TYPE_MAPPING);
    Element el = getChildByName(doc.getDocumentElement(), "mappings");
    Element[] _mappings = getChildren(el, "mapping");
    Map<String, Mapping> mappings = MapFactory.<String, Mapping>getConcurrentMap();
    Mapping tmp;
    boolean finished = false;
    if (configServer != null && config instanceof ConfigWeb) {
        Mapping[] sm = configServer.getMappings();
        for (int i = 0; i < sm.length; i++) {
            if (!sm[i].isHidden()) {
                if ("/".equals(sm[i].getVirtual()))
                    finished = true;
                if (sm[i] instanceof MappingImpl) {
                    tmp = ((MappingImpl) sm[i]).cloneReadOnly(config);
                    mappings.put(tmp.getVirtualLowerCase(), tmp);
                } else {
                    tmp = sm[i];
                    mappings.put(tmp.getVirtualLowerCase(), tmp);
                }
            }
        }
    }
    if (hasAccess) {
        boolean hasServerContext = false;
        for (int i = 0; i < _mappings.length; i++) {
            el = _mappings[i];
            String physical = el.getAttribute("physical");
            String archive = el.getAttribute("archive");
            String virtual = getAttr(el, "virtual");
            String listType = getAttr(el, "listener-type");
            String listMode = getAttr(el, "listener-mode");
            boolean readonly = toBoolean(getAttr(el, "readonly"), false);
            boolean hidden = toBoolean(getAttr(el, "hidden"), false);
            boolean toplevel = toBoolean(getAttr(el, "toplevel"), true);
            if (config instanceof ConfigServer && (virtual.equalsIgnoreCase("/lucee-server/") || virtual.equalsIgnoreCase("/lucee-server-context/"))) {
                hasServerContext = true;
            }
            // lucee
            if (virtual.equalsIgnoreCase("/lucee/")) {
                if (StringUtil.isEmpty(listType, true))
                    listType = "modern";
                if (StringUtil.isEmpty(listMode, true))
                    listMode = "curr2root";
                toplevel = true;
            }
            int listenerMode = ConfigWebUtil.toListenerMode(listMode, -1);
            int listenerType = ConfigWebUtil.toListenerType(listType, -1);
            ApplicationListener listener = ConfigWebUtil.loadListener(listenerType, null);
            if (listener != null || listenerMode != -1) {
                // type
                if (mode == ConfigImpl.MODE_STRICT)
                    listener = new ModernAppListener();
                else if (listener == null)
                    listener = ConfigWebUtil.loadListener(ConfigWebUtil.toListenerType(config.getApplicationListener().getType(), -1), null);
                if (// this should never be true
                listener == null)
                    listener = new ModernAppListener();
                // mode
                if (listenerMode == -1) {
                    listenerMode = config.getApplicationListener().getMode();
                }
                listener.setMode(listenerMode);
            }
            // physical!=null &&
            if ((physical != null || archive != null)) {
                short insTemp = inspectTemplate(el);
                if ("/lucee/".equalsIgnoreCase(virtual) || "/lucee".equalsIgnoreCase(virtual) || "/lucee-server/".equalsIgnoreCase(virtual) || "/lucee-server-context".equalsIgnoreCase(virtual))
                    insTemp = ConfigImpl.INSPECT_ONCE;
                String primary = getAttr(el, "primary");
                boolean physicalFirst = primary == null || !primary.equalsIgnoreCase("archive");
                tmp = new MappingImpl(config, virtual, physical, archive, insTemp, physicalFirst, hidden, readonly, toplevel, false, false, listener, listenerMode, listenerType);
                mappings.put(tmp.getVirtualLowerCase(), tmp);
                if (virtual.equals("/")) {
                    finished = true;
                // break;
                }
            }
        }
        // set default lucee-server-context
        if (config instanceof ConfigServer && !hasServerContext) {
            ApplicationListener listener = ConfigWebUtil.loadListener(ApplicationListener.TYPE_MODERN, null);
            listener.setMode(ApplicationListener.MODE_CURRENT2ROOT);
            tmp = new MappingImpl(config, "/lucee-server", "{lucee-server}/context/", null, ConfigImpl.INSPECT_ONCE, true, false, true, true, false, false, listener, ApplicationListener.MODE_CURRENT2ROOT, ApplicationListener.TYPE_MODERN);
            mappings.put(tmp.getVirtualLowerCase(), tmp);
        }
    }
    if (!finished) {
        if ((config instanceof ConfigWebImpl) && ResourceUtil.isUNCPath(config.getRootDirectory().getPath())) {
            tmp = new MappingImpl(config, "/", config.getRootDirectory().getPath(), null, ConfigImpl.INSPECT_UNDEFINED, true, true, true, true, false, false, null, -1, -1);
        } else {
            tmp = new MappingImpl(config, "/", "/", null, ConfigImpl.INSPECT_UNDEFINED, true, true, true, true, false, false, null, -1, -1);
        }
        mappings.put("/", tmp);
    }
    Mapping[] arrMapping = new Mapping[mappings.size()];
    int index = 0;
    Iterator it = mappings.keySet().iterator();
    while (it.hasNext()) {
        arrMapping[index++] = mappings.get(it.next());
    }
    config.setMappings(arrMapping);
// config.setMappings((Mapping[]) mappings.toArray(new
// Mapping[mappings.size()]));
}
Also used : ModernAppListener(lucee.runtime.listener.ModernAppListener) Element(org.w3c.dom.Element) Mapping(lucee.runtime.Mapping) MappingImpl(lucee.runtime.MappingImpl) lucee.aprint(lucee.aprint) ApplicationListener(lucee.runtime.listener.ApplicationListener) Iterator(java.util.Iterator)

Aggregations

MappingImpl (lucee.runtime.MappingImpl)18 Mapping (lucee.runtime.Mapping)11 Iterator (java.util.Iterator)4 Resource (lucee.commons.io.res.Resource)4 Entry (java.util.Map.Entry)3 lucee.aprint (lucee.aprint)3 ExtensionResourceFilter (lucee.commons.io.res.filter.ExtensionResourceFilter)3 Query (lucee.runtime.type.Query)3 QueryImpl (lucee.runtime.type.QueryImpl)3 Element (org.w3c.dom.Element)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 DirectoryResourceFilter (lucee.commons.io.res.filter.DirectoryResourceFilter)2 OrResourceFilter (lucee.commons.io.res.filter.OrResourceFilter)2 ResourceFilter (lucee.commons.io.res.filter.ResourceFilter)2 ApplicationException (lucee.runtime.exp.ApplicationException)2 Struct (lucee.runtime.type.Struct)2 IOException (java.io.IOException)1