/*
 * Copyright 2009-2010 by The Regents of the University of California
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * you may obtain a copy of the License from
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.uci.ics.hyracks.control.cc.application;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import edu.uci.ics.hyracks.api.application.ICCApplicationContext;
import edu.uci.ics.hyracks.api.application.ICCBootstrap;
import edu.uci.ics.hyracks.api.context.ICCContext;
import edu.uci.ics.hyracks.api.exceptions.HyracksException;
import edu.uci.ics.hyracks.api.job.IActivityClusterGraphGeneratorFactory;
import edu.uci.ics.hyracks.api.job.IJobLifecycleListener;
import edu.uci.ics.hyracks.api.job.JobId;
import edu.uci.ics.hyracks.api.util.JavaSerializationUtils;
import edu.uci.ics.hyracks.control.common.application.ApplicationContext;
import edu.uci.ics.hyracks.control.common.context.ServerContext;
import edu.uci.ics.hyracks.control.common.work.IResultCallback;

public class CCApplicationContext extends ApplicationContext implements ICCApplicationContext {
    private final ICCContext ccContext;

    protected final Set<String> initPendingNodeIds;
    protected final Set<String> deinitPendingNodeIds;

    protected IResultCallback<Object> initializationCallback;
    protected IResultCallback<Object> deinitializationCallback;

    private List<IJobLifecycleListener> jobLifecycleListeners;

    public CCApplicationContext(ServerContext serverCtx, ICCContext ccContext, String appName) throws IOException {
        super(serverCtx, appName);
        this.ccContext = ccContext;
        initPendingNodeIds = new HashSet<String>();
        deinitPendingNodeIds = new HashSet<String>();
        jobLifecycleListeners = new ArrayList<IJobLifecycleListener>();
    }

    @Override
    protected void start() throws Exception {
        ((ICCBootstrap) bootstrap).setApplicationContext(this);
        bootstrap.start();
    }

    public ICCContext getCCContext() {
        return ccContext;
    }

    public IActivityClusterGraphGeneratorFactory createActivityClusterGraphGeneratorFactory(byte[] bytes)
            throws HyracksException {
        try {
            return (IActivityClusterGraphGeneratorFactory) JavaSerializationUtils.deserialize(bytes, getClassLoader());
        } catch (IOException e) {
            throw new HyracksException(e);
        } catch (ClassNotFoundException e) {
            throw new HyracksException(e);
        }
    }

    @Override
    protected void stop() throws Exception {
        if (bootstrap != null) {
            bootstrap.stop();
        }
    }

    @Override
    public void setDistributedState(Serializable state) {
        this.distributedState = state;
    }

    @Override
    public void addJobLifecycleListener(IJobLifecycleListener jobLifecycleListener) {
        jobLifecycleListeners.add(jobLifecycleListener);
    }

    public synchronized void notifyJobStart(JobId jobId) throws HyracksException {
        for (IJobLifecycleListener l : jobLifecycleListeners) {
            l.notifyJobStart(jobId);
        }
    }

    public synchronized void notifyJobFinish(JobId jobId) throws HyracksException {
        for (IJobLifecycleListener l : jobLifecycleListeners) {
            l.notifyJobFinish(jobId);
        }
    }

    public synchronized void notifyJobCreation(JobId jobId, IActivityClusterGraphGeneratorFactory acggf)
            throws HyracksException {
        for (IJobLifecycleListener l : jobLifecycleListeners) {
            l.notifyJobCreation(jobId, acggf);
        }
    }

    public Set<String> getInitializationPendingNodeIds() {
        return initPendingNodeIds;
    }

    public Set<String> getDeinitializationPendingNodeIds() {
        return deinitPendingNodeIds;
    }

    public IResultCallback<Object> getInitializationCallback() {
        return initializationCallback;
    }

    public void setInitializationCallback(IResultCallback<Object> initializationCallback) {
        this.initializationCallback = initializationCallback;
    }

    public IResultCallback<Object> getDeinitializationCallback() {
        return deinitializationCallback;
    }

    public void setDeinitializationCallback(IResultCallback<Object> deinitializationCallback) {
        this.deinitializationCallback = deinitializationCallback;
    }
}
@@ -1005,32 +1005,97 @@
 ext.ICCContext;%0A
+import edu.uci.ics.hyracks.api.dataset.IDatasetDirectoryService;%0A
 import edu.uci.i
@@ -1134,24 +1134,24 @@
 sException;%0A
-
 import edu.u
@@ -1372,16 +1372,87 @@
 nUtils;%0A
+import edu.uci.ics.hyracks.control.cc.dataset.DatasetDirectoryService;%0A
 import e
@@ -2078,24 +2078,93 @@
 Listeners;%0A%0A
+    private final IDatasetDirectoryService datasetDirectoryService;%0A%0A
     public C
@@ -2436,32 +2436,32 @@
 hSet%3CString%3E();%0A
-
         jobLifec
@@ -2512,24 +2512,89 @@
 istener%3E();%0A
+        datasetDirectoryService = new DatasetDirectoryService();%0A
     %7D%0A%0A    @
@@ -5134,10 +5134,124 @@
 k;%0A    %7D
+%0A%0A    public IDatasetDirectoryService getDatasetDirectoryService() %7B%0A        return datasetDirectoryService;%0A    %7D
 %0A%7D

Create a DatasetDirectoryService per application in the application context.