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