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;
023
024import java.io.File;
025import java.io.IOException;
026import java.util.List;
027import java.util.Map;
028
029import javax.ws.rs.core.MediaType;
030
031import org.nuxeo.ecm.webengine.ResourceBinding;
032import org.nuxeo.ecm.webengine.WebEngine;
033import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
034import org.nuxeo.ecm.webengine.model.exceptions.WebSecurityException;
035import org.nuxeo.ecm.webengine.scripting.ScriptFile;
036
037/**
038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
039 */
040public interface Module extends MessagesProvider {
041
042    String getName();
043
044    File getRoot();
045
046    Resource getRootObject(WebContext ctx);
047
048    WebEngine getEngine();
049
050    void flushCache();
051
052    Module getSuperModule();
053
054    String getTemplateFileExt();
055
056    String getMediaTypeId(MediaType mt);
057
058    Map<String, String> getMessages(String language);
059
060    Messages getMessages();
061
062    List<ResourceBinding> getResourceBindings();
063
064    /**
065     * Gets a file using the configured directory stack. Each directory in the stack is asked for the file until a file
066     * is found. If no file is found, returns null.
067     * <p>
068     * Note that the implementation may cache the results. To clear any cached data, you should call the
069     * {@link #flushCache()} method.
070     *
071     * @param path the file path
072     * @return null if no file found otherwise the file
073     */
074    ScriptFile getFile(String path);
075
076    /**
077     * Gets a skin resource input stream. This must not cache resources. This method is using the module stacking
078     * directory to find the resource.
079     */
080    ScriptFile getSkinResource(String path) throws IOException;
081
082    /**
083     * Loads a class given its name.
084     * <p>
085     * The scripting class loader will be used to load the class.
086     *
087     * @param className the class name
088     * @return the class instance
089     */
090    Class<?> loadClass(String className) throws ClassNotFoundException;
091
092    /**
093     * Gets a {@link ResourceType} instance given its name.
094     * <p>
095     * The web type lookup is performed in the following order:
096     * <ol>
097     * <li>First the annotated Groovy classes are checked. (web/ directory)
098     * <li>Then the configuration type registry corresponding
099     * </ol>
100     *
101     * @param typeName the type name
102     * @return the web type instance
103     * @throws WebResourceNotFoundException if no such web type was defined
104     */
105    ResourceType getType(String typeName);
106
107    /**
108     * Gets the types registered within this module.
109     *
110     * @return the types. Cannot be null.
111     */
112    ResourceType[] getTypes();
113
114    /**
115     * Gets the adapters registered within this module.
116     *
117     * @return the adapters. Cannot be null.
118     */
119    AdapterType[] getAdapters();
120
121    /**
122     * Gets the named adapter definition for the given resource.
123     *
124     * @param ctx the target resource
125     * @param name the adapter name
126     * @return the adapter if any adapter with that name applies for that resource otherwise throws an exception
127     * @throws WebSecurityException if the adapter exists but cannot be accessed in the context of that resource
128     * @throws WebResourceNotFoundException if no such adapter exists for that resource
129     */
130    AdapterType getAdapter(Resource ctx, String name);
131
132    /**
133     * Gets the list of adapters that applies to the given resource.
134     *
135     * @param ctx the context resource
136     * @return the list of adapters Cannot be null.
137     */
138    List<AdapterType> getAdapters(Resource ctx);
139
140    /**
141     * Gets the list of adapter names that applies to the given resource.
142     *
143     * @param ctx the context resource
144     * @return the list of adapters Cannot be null.
145     */
146    List<String> getAdapterNames(Resource ctx);
147
148    /**
149     * Gets the list of adapters that are enabled for the given context.
150     * <p>
151     * Enabled adapters are those adapters which can be accessed in the current security context.
152     *
153     * @param ctx the context resource
154     * @return the list of adapter.s Cannot be null.
155     */
156    List<AdapterType> getEnabledAdapters(Resource ctx);
157
158    /**
159     * Gets the list of adapter names that are enabled for the given context.
160     * <p>
161     * Enabled services are those adapters which can be accessed in the current security context.
162     *
163     * @param ctx the context resource
164     * @return the list of adapters. Cannot be null.
165     */
166    List<String> getEnabledAdapterNames(Resource ctx);
167
168    List<LinkDescriptor> getLinks(String category);
169
170    List<LinkDescriptor> getActiveLinks(Resource context, String category);
171
172    /**
173     * Get the path prefix to be used from templates to prepend to links to static resources.
174     * <p>
175     * This prefix is exposed to templates as ${skinPath}.
176     *
177     * @return the skin path prefix. never null.
178     */
179    String getSkinPathPrefix();
180
181    boolean isDerivedFrom(String moduleName);
182
183}