001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine.model.impl;
023
024import java.io.File;
025import java.util.ArrayList;
026import java.util.HashSet;
027import java.util.List;
028import java.util.Set;
029
030import org.nuxeo.common.xmap.annotation.XNode;
031import org.nuxeo.common.xmap.annotation.XNodeList;
032import org.nuxeo.common.xmap.annotation.XObject;
033import org.nuxeo.ecm.webengine.ResourceBinding;
034import org.nuxeo.ecm.webengine.WebEngine;
035import org.nuxeo.ecm.webengine.app.WebEngineModule;
036import org.nuxeo.ecm.webengine.model.LinkDescriptor;
037import org.nuxeo.ecm.webengine.model.Module;
038import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
039
040/**
041 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
042 */
043@XObject("module")
044public class ModuleConfiguration implements Cloneable {
045
046    /**
047     * A web module may have multiple roots
048     *
049     * @deprecated you should use new module definition - through {@link WebEngineModule}
050     */
051    @Deprecated
052    @XNode("@path")
053    public String path;
054
055    /**
056     * @deprecated you should use new module definition - through {@link WebEngineModule}
057     */
058    @Deprecated
059    @XNode("@root-type")
060    public String rootType;
061
062    /**
063     * Paths of root resources in the module. This is replacing the deprecated root-type.
064     */
065    public Class<?>[] roots;
066
067    @XNode("@extends")
068    public String base;
069
070    @XNode("@name")
071    public String name;
072
073    /**
074     * Use module links instead. If a module doesn't declare a module item it will be headless by default. Still used
075     * for compatibility mode - for those modules not yet using moduleItems.
076     */
077    @Deprecated
078    @XNode("@headless")
079    public boolean isHeadless;
080
081    /**
082     * A list of entry points into the module - to be shown in the main webengine page. This is optional and may be
083     * ignored if your don't want to provide shortcuts to your module entry points.
084     */
085    @XNodeList(value = "shortcuts/shortcut", type = ArrayList.class, componentType = ModuleShortcut.class, nullByDefault = true)
086    public List<ModuleShortcut> moduleShortcuts;
087
088    /**
089     * Web Types explicitly declared. If null no web types were explicitly declared and old type loading method from the
090     * generated web-types file should be used.
091     */
092    public Class<?>[] types;
093
094    /**
095     * The module directory. Must be set by the client before registering the descriptor.
096     */
097    @XNode("home")
098    public File directory;
099
100    @XNodeList(value = "fragments/directory", type = ArrayList.class, componentType = File.class, nullByDefault = false)
101    public List<File> fragmentDirectories = new ArrayList<File>();
102
103    /**
104     * The module configuration file (this will be set by the module config parser)
105     */
106    public File file;
107
108    @XNodeList(value = "nature", type = HashSet.class, componentType = String.class, nullByDefault = true)
109    public Set<String> natures;
110
111    @XNodeList(value = "links/link", type = ArrayList.class, componentType = LinkDescriptor.class, nullByDefault = true)
112    public List<LinkDescriptor> links;
113
114    /**
115     * @deprecated resources are deprecated - you should use a jax-rs application to declare more resources.
116     */
117    @XNodeList(value = "resources/resource", type = ArrayList.class, componentType = ResourceBinding.class, nullByDefault = true)
118    public List<ResourceBinding> resources;
119
120    @XNode("templateFileExt")
121    public String templateFileExt = "ftl";
122
123    @XNodeList(value = "media-types/media-type", type = MediaTypeRef[].class, componentType = MediaTypeRef.class, nullByDefault = true)
124    public MediaTypeRef[] mediatTypeRefs;
125
126    public WebEngine engine;
127
128    private ModuleImpl module;
129
130    public boolean allowHostOverride;
131
132    public ModuleConfiguration() {
133    }
134
135    public ModuleConfiguration(WebEngine engine) {
136        this.engine = engine;
137    }
138
139    public WebEngine getEngine() {
140        return engine;
141    }
142
143    public void setEngine(WebEngine engine) {
144        this.engine = engine;
145    }
146
147    public String getName() {
148        return name;
149    }
150
151    public List<ModuleShortcut> getShortcuts() {
152        return moduleShortcuts;
153    }
154
155    public List<LinkDescriptor> getLinks() {
156        return links;
157    }
158
159    public File getDirectory() {
160        return directory;
161    }
162
163    public String getBase() {
164        return base;
165    }
166
167    /**
168     * @deprecated you should use new module definition - through {@link WebEngineModule}
169     */
170    @Deprecated
171    public String getPath() {
172        return path;
173    }
174
175    public Module get() {
176        if (module == null) {
177            Module superModule = null;
178            if (base != null) { // make sure super modules are resolved
179                ModuleConfiguration superM = engine.getModuleManager().getModule(base);
180                if (superM == null) {
181                    throw new WebResourceNotFoundException("The module '" + name
182                            + "' cannot be loaded since it's super module '" + base + "' cannot be found");
183                }
184                // force super module loading
185                superModule = superM.get();
186            }
187            module = new ModuleImpl(engine, (ModuleImpl) superModule, this);
188        }
189        return module;
190    }
191
192    public boolean isLoaded() {
193        return module != null;
194    }
195
196    public boolean isHeadless() {
197        return isHeadless;
198    }
199
200}