net.navasoft.madcoin.backend.services.rest.impl.NotificationMaster.java Source code

Java tutorial

Introduction

Here is the source code for net.navasoft.madcoin.backend.services.rest.impl.NotificationMaster.java

Source

/*******************************************************************************
 * Copyright 2014 Juan Diego Navarre Gonzalez
 * 
 * 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 at
 * 
 *   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 net.navasoft.madcoin.backend.services.rest.impl;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;

import net.navasoft.madcoin.backend.model.entities.impl.WorkRequests;
import net.navasoft.madcoin.backend.services.rest.Slave;
import net.navasoft.madcoin.backend.services.rest.SlaveFactory;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.stereotype.Component;

/**
 * net.navasoft.madcoin.backend.services.rest.impl Class class
 * NotificationMaster. Description:
 * 
 * @author Juan Diego Navarre Gonzalez - (${authorMail})
 * @version 1.0
 * @since 3/09/2014 11:35:49 PM
 */
@Component("pushMaster")
@org.springframework.context.annotation.Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class NotificationMaster extends Master<Provider> {

    /**
     * Constant journal.
     * 
     * @since 3/09/2014, 11:35:49 PM
     */
    private static final Logger journal = Logger.getLogger(NotificationMaster.class);

    /**
     * factory.
     * 
     * @since 3/09/2014, 11:35:49 PM
     */
    @Autowired
    @Qualifier("slaveFactory")
    private SlaveFactory<Provider> factory;

    /**
     * Instantiates a new notification master.
     * 
     * @since 3/09/2014, 11:35:49 PM
     */
    public NotificationMaster() {
        slaves = new ArrayList<Slave<Provider, ?>>();
    }

    /**
     * Inits the.
     * 
     * @param initializer
     *            the initializer
     * @since 7/09/2014, 09:08:10 PM
     */
    public void init(Serializable initializer) {
        super.setScope(new ProviderScope<Provider>((WorkRequests) initializer));
    }

    /**
     * Distribute.
     * 
     * @throws Exception
     *             the exception
     * @since 3/09/2014, 11:35:49 PM
     */
    @Override
    public void distribute() throws Exception {
        int slaveTimes = getStrategy().getRate().getWorkerLoad().intValue();
        int carga = getStrategy().getRate().getTotalLoad().intValue();
        Provider[] total;
        try {
            total = (Provider[]) Array.newInstance(getParameterizedType(), 0);
            total = population.toArray(total);
            for (int i = 0; i < total.length;) {
                for (int j = 0; j < slaveTimes; j++) {
                    slaves.add((Slave<Provider, ?>) createBlankSlave());
                }
                i = i + carga;
                // total = (Oficina[])ArrayUtils.remove(total, i);
            }
            int k = 0;
            for (Slave<Provider, ?> worker : slaves) {
                prepareSlave(worker);
                putLoad(worker, total[k]);
                k++;
            }
        } catch (NegativeArraySizeException | ClassNotFoundException e) {
            journal.error(e);
        }
    }

    /**
     * Put load.
     * 
     * @param slave
     *            the slave
     * @param work
     *            the work
     * @return the slave
     * @throws Exception
     *             the exception
     * @since 3/09/2014, 11:35:49 PM
     */
    @Override
    public Slave<Provider, ?> putLoad(Slave<Provider, ?> slave, Provider work) throws Exception {
        journal.info("Asignando " + work.getProviderName());
        slave.setWorkLoad(work);
        slave.setTaskId(work.getProviderName());
        slave.setup();
        super.control.put(work.getProviderName(), slave);
        return slave;
    }

    /**
     * Finish.
     * 
     * @since 3/09/2014, 11:35:49 PM
     */
    @Override
    public void finish() {
        journal.info("Terminando....");
    }

    /**
     * Process.
     * 
     * @since 3/09/2014, 11:35:49 PM
     */
    @Override
    public void process() {
        for (Slave<Provider, ?> worker : slaves) {
            control.put(worker.getTaskId(), new Thread(worker, worker.getTaskId()));
            ((Thread) control.get(worker.getTaskId())).start();
        }
    }

    /**
     * Gets the parameterized type.
     * 
     * @return the parameterized type
     * @throws ClassNotFoundException
     *             the class not found exception
     * @since 3/09/2014, 11:35:49 PM
     */
    @SuppressWarnings("unchecked")
    @Override
    protected Class<Provider> getParameterizedType() throws ClassNotFoundException {
        String superClass = ((ParameterizedType) NotificationMaster.class.getGenericSuperclass())
                .getActualTypeArguments()[0].toString();
        return (Class<Provider>) Class.forName(superClass.split(" ")[1].trim());
    }

    /**
     * Sets the factory.
     * 
     * @param fab
     *            the new factory
     * @since 3/09/2014, 11:35:49 PM
     */
    @Override
    public void setFactory(SlaveFactory<Provider> fab) {
        this.factory = fab;
    }

    /**
     * Gets the factory.
     * 
     * @return the factory
     * @since 3/09/2014, 11:35:49 PM
     */
    @Override
    public SlaveFactory<Provider> getFactory() {
        return this.factory;
    }

}