001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.oozie.dependency;
020
021import java.net.URI;
022import java.util.List;
023import java.util.Set;
024
025import org.apache.hadoop.conf.Configuration;
026import org.apache.oozie.action.hadoop.LauncherURIHandler;
027
028public interface URIHandler {
029
030    /**
031     * Type of the dependency. PULL dependencies are those whose availability is determined by
032     * polling and PUSH dependencies are those whose availability is known from notifications
033     */
034    public enum DependencyType {
035        PULL,
036        PUSH;
037    }
038
039    /**
040     * Initialize the URIHandler
041     *
042     * @param conf Configuration for initialization
043     */
044    public void init(Configuration conf);
045
046    /**
047     * Get the list of uri schemes supported by this URIHandler
048     *
049     * @return supported list of uri schemes
050     */
051    public Set<String> getSupportedSchemes();
052
053    /**
054     * Get the URIHandler that will be used to handle the supported schemes in launcher
055     *
056     * @return LauncherURIHandler that handles URI in the launcher
057     */
058    public Class<? extends LauncherURIHandler> getLauncherURIHandlerClass();
059
060    /**
061     * Get list of classes to ship to launcher for LauncherURIHandler
062     *
063     * @return list of classes to ship to launcher
064     */
065    public List<Class<?>> getClassesForLauncher();
066
067    /**
068     * Get the dependency type of the URI. When the availability of the
069     * URI is to be determined by polling the type is DependencyType.PULL, and
070     * when the availability is received through notifications from a external
071     * entity like a JMS server the type is DependencyType.PUSH
072     *
073     * @return dependency type of URI
074     */
075    public DependencyType getDependencyType(URI uri) throws URIHandlerException;
076
077    /**
078     * Register for notifications in case of a push dependency
079     *
080     * @param uri  The URI to check for availability
081     * @param conf Configuration to access the URI
082     * @param user name of the user the URI should be accessed as
083     * @param actionID The id of action which depends on the availability of the uri
084     *
085     * @throws URIHandlerException
086     */
087    public void registerForNotification(URI uri, Configuration conf, String user, String actionID)
088            throws URIHandlerException;
089
090    /**
091     * Unregister from notifications in case of a push dependency
092     *
093     * @param uri The URI to be removed from missing dependency
094     * @param actionID The id of action which was dependent on the uri.
095     *
096     * @throws URIHandlerException
097     */
098    public boolean unregisterFromNotification(URI uri, String actionID);
099
100    /**
101     * Get the Context which can be used to access URI of the same scheme and
102     * host
103     *
104     * @param uri URI which identifies the scheme and host
105     * @param conf Configuration to access the URI
106     * @param user name of the user the URI should be accessed as
107     *
108     * @return Context to access URIs with same scheme and host
109     *
110     * @throws URIHandlerException
111     */
112    public Context getContext(URI uri, Configuration conf, String user) throws URIHandlerException;
113
114    /**
115     * Check if the dependency identified by the URI is available
116     *
117     * @param uri URI of the dependency
118     * @param context Context to access the URI
119     *
120     * @return <code>true</code> if the URI exists; <code>false</code> if the
121     *         URI does not exist
122     *
123     * @throws URIHandlerException
124     */
125    public boolean exists(URI uri, Context context) throws URIHandlerException;
126
127    /**
128     * Check if the dependency identified by the URI is available
129     *
130     * @param uri URI of the dependency
131     * @param conf Configuration to access the URI
132     * @param user name of the user the URI should be accessed as. If null the
133     *        logged in user is used.
134     *
135     * @return <code>true</code> if the URI exists; <code>false</code> if the
136     *         URI does not exist
137     *
138     * @throws URIHandlerException
139     */
140    public boolean exists(URI uri, Configuration conf, String user) throws URIHandlerException;
141
142    /**
143     * Get the URI based on the done flag
144     *
145     * @param uri URI of the dependency
146     * @param doneFlag flag that determines URI availability
147     *
148     * @return the final URI with the doneFlag incorporated
149     *
150     * @throws URIHandlerException
151     */
152    public String getURIWithDoneFlag(String uri, String doneFlag) throws URIHandlerException;
153
154    /**
155     * Check whether the URI is valid or not
156     * @param uri
157     * @return
158     * @throws URIHandlerException
159     */
160    public void validate(String uri) throws URIHandlerException;
161
162    /**
163     * Destroy the URIHandler
164     */
165    public void destroy();
166
167    public static abstract class Context {
168
169        private Configuration conf;
170        private String user;
171
172        /**
173         * Create a Context that can be used to access a URI
174         *
175         * @param conf Configuration to access the URI
176         * @param user name of the user the URI should be accessed as
177         */
178        public Context(Configuration conf, String user) {
179            this.conf = conf;
180            this.user = user;
181        }
182
183        /**
184         * Get the Configuration to access the URI
185         * @return Configuration to access the URI
186         */
187        public Configuration getConfiguration() {
188            return conf;
189        }
190
191        /**
192         * Get the name of the user the URI will be accessed as
193         * @return the user name the URI will be accessed as
194         */
195        public String getUser() {
196            return user;
197        }
198
199        /**
200         * Destroy the Context
201         */
202        public void destroy() {
203        }
204
205    }
206
207}