Search in sources :

Example 6 with ExtensionDefintion

use of lucee.runtime.extension.ExtensionDefintion in project Lucee by lucee.

the class Admin method doGetLocalExtension.

private void doGetLocalExtension() throws PageException {
    String id = getString("admin", action, "id");
    boolean asBinary = getBoolV("asBinary", false);
    if (asBinary) {
        Iterator<ExtensionDefintion> it = DeployHandler.getLocalExtensions(config).iterator();
        ExtensionDefintion ext;
        while (it.hasNext()) {
            ext = it.next();
            if (id.equalsIgnoreCase(ext.getId())) {
                try {
                    pageContext.setVariable(getString("admin", action, "returnVariable"), IOUtil.toBytes(ext.getSource()));
                    return;
                } catch (IOException e) {
                    throw Caster.toPageException(e);
                }
            }
        }
        throw new ApplicationException("there is no local extension with id " + id);
    } else {
        List<RHExtension> locals = RHExtension.toRHExtensions(DeployHandler.getLocalExtensions(config));
        Query qry = RHExtension.toQuery(config, locals.toArray(new RHExtension[locals.size()]));
        int rows = qry.getRecordcount();
        String _id;
        int row = 0;
        for (int r = 1; r <= rows; r++) {
            _id = Caster.toString(qry.getAt(KeyConstants._id, r), null);
            if (id.equalsIgnoreCase(_id)) {
                row = r;
                break;
            }
        }
        if (row == 0)
            throw new ApplicationException("there is no local extension with id " + id);
        pageContext.setVariable(getString("admin", action, "returnVariable"), Caster.toStruct(qry, row));
    }
}
Also used : RHExtension(lucee.runtime.extension.RHExtension) ApplicationException(lucee.runtime.exp.ApplicationException) Query(lucee.runtime.type.Query) ExtensionDefintion(lucee.runtime.extension.ExtensionDefintion) IOException(java.io.IOException)

Example 7 with ExtensionDefintion

use of lucee.runtime.extension.ExtensionDefintion in project Lucee by lucee.

the class CFMLEngineImpl method deployBundledExtensionZip.

private void deployBundledExtensionZip(ConfigServerImpl cs) {
    Resource dir = cs.getLocalExtensionProviderDirectory();
    List<ExtensionDefintion> existing = DeployHandler.getLocalExtensions(cs);
    String sub = "extensions/";
    // MUST this does not work on windows! we need to add an index
    ZipEntry entry;
    ZipInputStream zis = null;
    try {
        CodeSource src = CFMLEngineFactory.class.getProtectionDomain().getCodeSource();
        if (src == null)
            return;
        URL loc = src.getLocation();
        zis = new ZipInputStream(loc.openStream());
        String path, name;
        int index;
        Resource temp;
        RHExtension rhe;
        Iterator<ExtensionDefintion> it;
        ExtensionDefintion exist;
        while ((entry = zis.getNextEntry()) != null) {
            path = entry.getName();
            if (path.startsWith(sub) && path.endsWith(".lex")) {
                // ignore non lex files or file from else where
                index = path.lastIndexOf('/') + 1;
                if (index == sub.length()) {
                    // ignore sub directories
                    name = path.substring(index);
                    temp = null;
                    try {
                        temp = SystemUtil.getTempDirectory().getRealResource(name);
                        ResourceUtil.touch(temp);
                        Util.copy(zis, temp.getOutputStream(), false, true);
                        rhe = new RHExtension(cs, temp, false);
                        boolean alreadyExists = false;
                        it = existing.iterator();
                        while (it.hasNext()) {
                            exist = it.next();
                            if (exist.equals(rhe)) {
                                alreadyExists = true;
                                break;
                            }
                        }
                        if (!alreadyExists) {
                            temp.moveTo(dir.getRealResource(name));
                        }
                    } finally {
                        if (temp != null && temp.exists())
                            temp.delete();
                    }
                }
            }
            zis.closeEntry();
        }
    } catch (Throwable t) {
        // TODO log this
        ExceptionUtil.rethrowIfNecessary(t);
    } finally {
        Util.closeEL(zis);
    }
    return;
}
Also used : ZipEntry(java.util.zip.ZipEntry) Resource(lucee.commons.io.res.Resource) CodeSource(java.security.CodeSource) URL(java.net.URL) RHExtension(lucee.runtime.extension.RHExtension) ZipInputStream(java.util.zip.ZipInputStream) ExtensionDefintion(lucee.runtime.extension.ExtensionDefintion) CFMLEngineFactory(lucee.loader.engine.CFMLEngineFactory)

Example 8 with ExtensionDefintion

use of lucee.runtime.extension.ExtensionDefintion in project Lucee by lucee.

the class DeployHandler method getLocalExtension.

public static ExtensionDefintion getLocalExtension(Config config, ExtensionDefintion ed, ExtensionDefintion defaultValue) {
    Iterator<ExtensionDefintion> it = getLocalExtensions(config).iterator();
    ExtensionDefintion ext;
    while (it.hasNext()) {
        ext = it.next();
        if (ed.equals(ext)) {
            return ext;
        }
    }
    return defaultValue;
}
Also used : ExtensionDefintion(lucee.runtime.extension.ExtensionDefintion)

Example 9 with ExtensionDefintion

use of lucee.runtime.extension.ExtensionDefintion in project Lucee by lucee.

the class DeployHandler method deployExtension.

/**
 * install a extension based on the given id and version
 * @param config
 * @param id the id of the extension
 * @param version pass null if you don't need a specific version
 * @return
 * @throws IOException
 * @throws PageException
 */
public static boolean deployExtension(Config config, ExtensionDefintion ed, Log log, boolean reload) {
    ConfigImpl ci = (ConfigImpl) config;
    // is the extension already installed
    try {
        if (XMLConfigAdmin.hasRHExtensions(ci, ed) != null)
            return false;
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
    }
    // check if a local extension is matching our id
    Iterator<ExtensionDefintion> it = getLocalExtensions(config).iterator();
    ExtensionDefintion ext = null, tmp;
    log.info("extension", "installing the extension " + ed);
    while (it.hasNext()) {
        tmp = it.next();
        if (ed.equals(tmp)) {
            ext = tmp;
            break;
        }
    }
    // if we have one and also the defined version matches, there is no need to check online
    if (ext != null && ed.getVersion() != null) {
        try {
            log.info("extension", "installing the extension " + ed + " from local provider");
            Resource res = SystemUtil.getTempDirectory().getRealResource(ed.getId() + "-" + ed.getVersion() + ".lex");
            ResourceUtil.touch(res);
            IOUtil.copy(ext.getSource(), res);
            XMLConfigAdmin._updateRHExtension((ConfigImpl) config, res, reload);
            return true;
        } catch (Exception e) {
            ext = null;
            SystemOut.printDate(e);
        }
    }
    String apiKey = config.getIdentification().getApiKey();
    RHExtensionProvider[] providers = ci.getRHExtensionProviders();
    URL url;
    // if we have a local version, we look if there is a newer remote version
    if (ext != null) {
        String content;
        for (int i = 0; i < providers.length; i++) {
            HTTPResponse rsp = null;
            try {
                url = providers[i].getURL();
                StringBuilder qs = new StringBuilder();
                qs.append("?withLogo=false");
                if (ed.getVersion() != null)
                    qs.append("&version=").append(ed.getVersion());
                if (apiKey != null)
                    qs.append("&ioid=").append(apiKey);
                url = new URL(url, "/rest/extension/provider/info/" + ed.getId() + qs);
                rsp = HTTPEngine.get(url, null, null, -1, false, "UTF-8", "", null, new Header[] { new HeaderImpl("accept", "application/json") });
                if (rsp.getStatusCode() != 200)
                    continue;
                content = rsp.getContentAsString();
                Struct sct = Caster.toStruct(DeserializeJSON.call(null, content));
                String remoteVersion = Caster.toString(sct.get(KeyConstants._version));
                // the local version is as good as the remote
                if (remoteVersion != null && remoteVersion.compareTo(ext.getVersion()) <= 0) {
                    log.info("extension", "installing the extension " + ed + " from local provider");
                    // avoid that the exzension from provider get removed
                    Resource res = SystemUtil.getTempDirectory().getRealResource(ed.getId() + "-" + ed.getVersion() + ".lex");
                    ResourceUtil.touch(res);
                    IOUtil.copy(ext.getSource(), res);
                    XMLConfigAdmin._updateRHExtension((ConfigImpl) config, res, reload);
                    return true;
                }
            } catch (Throwable t) {
                ExceptionUtil.rethrowIfNecessary(t);
            } finally {
                HTTPEngine.closeEL(rsp);
            }
        }
    }
    // if we have an ext at this stage this mean the remote providers was not acessible or have not this extension
    if (ext != null) {
        try {
            log.info("extension", "installing the extension " + ed + " from local provider");
            Resource res = SystemUtil.getTempDirectory().getRealResource(ext.getSource().getName());
            ResourceUtil.touch(res);
            IOUtil.copy(ext.getSource(), res);
            XMLConfigAdmin._updateRHExtension((ConfigImpl) config, res, reload);
            return true;
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
        }
    }
    // if not we try to download it
    log.info("extension", "installing the extension " + ed + " from remote extension provider");
    Resource res = downloadExtension(ci, ed, log);
    if (res != null) {
        try {
            XMLConfigAdmin._updateRHExtension((ConfigImpl) config, res, reload);
            return true;
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
            log.error("extension", t);
        }
    }
    return false;
}
Also used : HeaderImpl(lucee.commons.net.http.httpclient.HeaderImpl) HTTPResponse(lucee.commons.net.http.HTTPResponse) Resource(lucee.commons.io.res.Resource) PageException(lucee.runtime.exp.PageException) IOException(java.io.IOException) URL(java.net.URL) RHExtensionProvider(lucee.runtime.extension.RHExtensionProvider) Struct(lucee.runtime.type.Struct) Header(lucee.commons.net.http.Header) ExtensionDefintion(lucee.runtime.extension.ExtensionDefintion)

Aggregations

ExtensionDefintion (lucee.runtime.extension.ExtensionDefintion)9 Resource (lucee.commons.io.res.Resource)5 IOException (java.io.IOException)4 RHExtension (lucee.runtime.extension.RHExtension)4 ApplicationException (lucee.runtime.exp.ApplicationException)3 PageException (lucee.runtime.exp.PageException)3 URL (java.net.URL)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ZipInputStream (java.util.zip.ZipInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 CodeSource (java.security.CodeSource)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Iterator (java.util.Iterator)1 LinkedHashSet (java.util.LinkedHashSet)1 ZipEntry (java.util.zip.ZipEntry)1 Log (lucee.commons.io.log.Log)1 ExtensionResourceFilter (lucee.commons.io.res.filter.ExtensionResourceFilter)1