Search in sources :

Example 1 with EventHandler

use of org.osgi.service.event.EventHandler in project tdi-studio-se by Talend.

the class StandAloneTalendJavaEditor method init.

@Override
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
    super.init(site, input);
    Bundle bundle = FrameworkUtil.getBundle(StandAloneTalendJavaEditor.class);
    lockService = bundle.getBundleContext().registerService(EventHandler.class.getName(), new EventHandler() {

        @Override
        public void handleEvent(Event event) {
            String lockTopic = Constant.REPOSITORY_ITEM_EVENT_PREFIX + Constant.ITEM_LOCK_EVENT_SUFFIX;
            if (lockTopic.equals(event.getTopic())) {
                if (!isEditable) {
                    Object o = event.getProperty(Constant.ITEM_EVENT_PROPERTY_KEY);
                    if (o instanceof FileItem) {
                        boolean isTheCorrectEditor = false;
                        Property property = ((FileItem) o).getProperty();
                        if (property != null) {
                            String eventItemId = property.getId();
                            //$NON-NLS-1$
                            String currentOpenedItemId = "";
                            if (rEditorInput != null) {
                                currentOpenedItemId = rEditorInput.getId();
                            }
                            isTheCorrectEditor = currentOpenedItemId.equals(eventItemId);
                        }
                        if (!isTheCorrectEditor) {
                            return;
                        }
                        item.getProperty().eAdapters().remove(dirtyListener);
                        item = (FileItem) o;
                        item.getProperty().eAdapters().add(dirtyListener);
                        if (isEditable()) {
                            isEditable = true;
                            rEditorInput.getFile().setReadOnly(false);
                            getSite().getShell().getDisplay().asyncExec(new Runnable() {

                                @Override
                                public void run() {
                                    setFocus();
                                    ISourceViewer viewer = getViewer();
                                    if (viewer != null) {
                                        StyledText styledText = viewer.getTextWidget();
                                        if (styledText != null) {
                                            styledText.setBackground(bgColorForEditabeItem);
                                            styledText.setDragDetect(true);
                                        }
                                    }
                                }
                            });
                            try {
                                ICodeGeneratorService service = (ICodeGeneratorService) GlobalServiceRegister.getDefault().getService(ICodeGeneratorService.class);
                                if (o instanceof RoutineItem) {
                                    ITalendSynchronizer routineSynchronizer = service.createJavaRoutineSynchronizer();
                                    routineSynchronizer.syncRoutine((RoutineItem) o, true);
                                } else if (o instanceof SQLPatternItem) {
                                    ISQLPatternSynchronizer sqlPatternSynchronizer = service.getSQLPatternSynchronizer();
                                    sqlPatternSynchronizer.syncSQLPattern((SQLPatternItem) o, true);
                                } else {
                                    //$NON-NLS-1$
                                    org.talend.commons.exception.ExceptionHandler.process(new Exception("Uncatched case"));
                                }
                                setName();
                            } catch (Exception e) {
                                org.talend.commons.exception.ExceptionHandler.process(e);
                            }
                        }
                    }
                }
            }
        }
    }, new Hashtable<String, String>(//$NON-NLS-1$
    Collections.singletonMap(EventConstants.EVENT_TOPIC, Constant.REPOSITORY_ITEM_EVENT_PREFIX + "*")));
}
Also used : ITalendSynchronizer(org.talend.designer.codegen.ITalendSynchronizer) StyledText(org.eclipse.swt.custom.StyledText) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) ISQLPatternSynchronizer(org.talend.designer.codegen.ISQLPatternSynchronizer) EventHandler(org.osgi.service.event.EventHandler) RoutineItem(org.talend.core.model.properties.RoutineItem) SQLPatternItem(org.talend.core.model.properties.SQLPatternItem) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) LoginException(org.talend.commons.exception.LoginException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PersistenceException(org.talend.commons.exception.PersistenceException) ICodeGeneratorService(org.talend.designer.codegen.ICodeGeneratorService) FileItem(org.talend.core.model.properties.FileItem) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IResourceChangeEvent(org.eclipse.core.resources.IResourceChangeEvent) ResourceChangeEvent(org.eclipse.core.internal.events.ResourceChangeEvent) Event(org.osgi.service.event.Event) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) Property(org.talend.core.model.properties.Property)

Example 2 with EventHandler

use of org.osgi.service.event.EventHandler in project tdi-studio-se by Talend.

the class AbstractMultiPageTalendEditor method init.

/*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.part.MultiPageEditorPart#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
     */
@Override
public void init(final IEditorSite site, IEditorInput editorInput) throws PartInitException {
    setSite(site);
    setInput(editorInput);
    if (!(editorInput instanceof JobEditorInput)) {
        return;
    }
    site.setSelectionProvider(new MultiPageTalendSelectionProvider(this));
    getSite().getWorkbenchWindow().getSelectionService().addSelectionListener(this);
    // Lock the process :
    IRepositoryService service = CorePlugin.getDefault().getRepositoryService();
    final IProxyRepositoryFactory repFactory = service.getProxyRepositoryFactory();
    processEditorInput = (JobEditorInput) editorInput;
    final IProcess2 currentProcess = processEditorInput.getLoadedProcess();
    if (!currentProcess.isReadOnly()) {
        try {
            Property property = processEditorInput.getItem().getProperty();
            propertyInformation = new ArrayList(property.getInformations());
            property.eAdapters().add(dirtyListener);
            repFactory.lock(currentProcess);
            boolean locked = repFactory.getStatus(currentProcess) == ERepositoryStatus.LOCK_BY_USER;
            if (!locked) {
                setReadOnly(true);
            }
            revisionChanged = true;
        } catch (PersistenceException e) {
            // e.printStackTrace();
            ExceptionHandler.process(e);
        } catch (BusinessException e) {
            // Nothing to do
            ExceptionHandler.process(e);
        }
    } else {
        setReadOnly(true);
        Bundle bundle = FrameworkUtil.getBundle(AbstractMultiPageTalendEditor.class);
        final Display display = getSite().getShell().getDisplay();
        this.lockService = bundle.getBundleContext().registerService(EventHandler.class.getName(), new EventHandler() {

            @Override
            public void handleEvent(Event event) {
                String lockTopic = Constant.REPOSITORY_ITEM_EVENT_PREFIX + Constant.ITEM_LOCK_EVENT_SUFFIX;
                if (lockTopic.equals(event.getTopic())) {
                    Object o = event.getProperty(Constant.ITEM_EVENT_PROPERTY_KEY);
                    if (o != null && o instanceof Item) {
                        Item item = (Item) o;
                        String itemId = item.getProperty().getId();
                        if (itemId.equals(currentProcess.getId())) {
                            if (currentProcess.isReadOnly()) {
                                boolean readOnly = currentProcess.checkReadOnly();
                                boolean orginalReadOnlyStatus = designerEditor.isReadOnly();
                                setReadOnly(readOnly);
                                if (!readOnly) {
                                    display.asyncExec(new Runnable() {

                                        @Override
                                        public void run() {
                                            setFocus();
                                        }
                                    });
                                    if (orginalReadOnlyStatus == true) {
                                        // refresh to the given item version, nomally it is the latest
                                        // version,
                                        // means the editor/process will be refreshed to the latest version
                                        refreshProcess(item, false);
                                    }
                                    Property property = processEditorInput.getItem().getProperty();
                                    propertyInformation = new ArrayList(property.getInformations());
                                    property.eAdapters().add(dirtyListener);
                                }
                            }
                        }
                    }
                }
            }
        }, new Hashtable<String, String>(Collections.singletonMap(EventConstants.EVENT_TOPIC, //$NON-NLS-1$
        Constant.REPOSITORY_ITEM_EVENT_PREFIX + "*")));
        revisionChanged = true;
    }
    // setTitleImage(ImageProvider.getImage(getEditorTitleImage()));
    updateTitleImage(processEditorInput.getItem().getProperty());
    getSite().getWorkbenchWindow().getPartService().addPartListener(partListener);
}
Also used : Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) EventHandler(org.osgi.service.event.EventHandler) IRepositoryService(org.talend.repository.model.IRepositoryService) JobEditorInput(org.talend.core.ui.editor.JobEditorInput) JobletProcessItem(org.talend.core.model.properties.JobletProcessItem) ProcessItem(org.talend.core.model.properties.ProcessItem) Item(org.talend.core.model.properties.Item) BusinessException(org.talend.commons.exception.BusinessException) IProcess2(org.talend.core.model.process.IProcess2) PersistenceException(org.talend.commons.exception.PersistenceException) IResourceChangeEvent(org.eclipse.core.resources.IResourceChangeEvent) Event(org.osgi.service.event.Event) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject) PlatformObject(org.eclipse.core.runtime.PlatformObject) IDynamicProperty(org.talend.core.ui.properties.tab.IDynamicProperty) Property(org.talend.core.model.properties.Property) IProxyRepositoryFactory(org.talend.repository.model.IProxyRepositoryFactory) Display(org.eclipse.swt.widgets.Display)

Example 3 with EventHandler

use of org.osgi.service.event.EventHandler in project sling by apache.

the class TopicMatchingTest method testDeepMatching.

/**
     * Test deep pattern matching /**
     */
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testDeepMatching() throws Exception {
    final Barrier barrier = new Barrier(2);
    this.registerJobExecutor("sling/**", new JobExecutor() {

        @Override
        public JobExecutionResult process(final Job job, final JobExecutionContext context) {
            return context.result().succeeded();
        }
    });
    this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED, new EventHandler() {

        @Override
        public void handleEvent(final Event event) {
            barrier.block();
        }
    });
    this.getJobManager().addJob(TOPIC, null);
    barrier.block();
}
Also used : JobExecutionResult(org.apache.sling.event.jobs.consumer.JobExecutionResult) JobExecutor(org.apache.sling.event.jobs.consumer.JobExecutor) JobExecutionContext(org.apache.sling.event.jobs.consumer.JobExecutionContext) EventHandler(org.osgi.service.event.EventHandler) Event(org.osgi.service.event.Event) Barrier(org.apache.sling.event.impl.Barrier) Job(org.apache.sling.event.jobs.Job) Test(org.junit.Test)

Example 4 with EventHandler

use of org.osgi.service.event.EventHandler in project sling by apache.

the class TopicMatchingTest method testSimpleMatching.

/**
     * Test simple pattern matching /*
     */
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testSimpleMatching() throws Exception {
    final Barrier barrier = new Barrier(2);
    this.registerJobExecutor("sling/test/*", new JobExecutor() {

        @Override
        public JobExecutionResult process(final Job job, final JobExecutionContext context) {
            return context.result().succeeded();
        }
    });
    this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED, new EventHandler() {

        @Override
        public void handleEvent(final Event event) {
            barrier.block();
        }
    });
    this.getJobManager().addJob(TOPIC, null);
    barrier.block();
}
Also used : JobExecutionResult(org.apache.sling.event.jobs.consumer.JobExecutionResult) JobExecutor(org.apache.sling.event.jobs.consumer.JobExecutor) JobExecutionContext(org.apache.sling.event.jobs.consumer.JobExecutionContext) EventHandler(org.osgi.service.event.EventHandler) Event(org.osgi.service.event.Event) Barrier(org.apache.sling.event.impl.Barrier) Job(org.apache.sling.event.jobs.Job) Test(org.junit.Test)

Example 5 with EventHandler

use of org.osgi.service.event.EventHandler in project sling by apache.

the class UnorderedQueueTest method testUnorderedQueue.

@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testUnorderedQueue() throws Exception {
    final JobManager jobManager = this.getJobManager();
    final Barrier cb = new Barrier(2);
    this.registerJobConsumer(TOPIC + "/start", new JobConsumer() {

        @Override
        public JobResult process(final Job job) {
            cb.block();
            return JobResult.OK;
        }
    });
    // register new consumer and event handle
    final AtomicInteger count = new AtomicInteger(0);
    final AtomicInteger parallelCount = new AtomicInteger(0);
    final Set<Integer> maxParticipants = new HashSet<Integer>();
    this.registerJobConsumer(TOPIC + "/*", new JobConsumer() {

        @Override
        public JobResult process(final Job job) {
            final int max = parallelCount.incrementAndGet();
            if (max > MAX_PAR) {
                parallelCount.decrementAndGet();
                return JobResult.FAILED;
            }
            synchronized (maxParticipants) {
                maxParticipants.add(max);
            }
            sleep(job.getProperty("sleep", 30));
            parallelCount.decrementAndGet();
            return JobResult.OK;
        }
    });
    this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED, new EventHandler() {

        @Override
        public void handleEvent(final Event event) {
            count.incrementAndGet();
        }
    });
    // we first sent one event to get the queue started
    jobManager.addJob(TOPIC + "/start", null);
    assertTrue("No event received in the given time.", cb.block(5));
    cb.reset();
    // get the queue
    final Queue q = jobManager.getQueue(QUEUE_NAME);
    assertNotNull("Queue '" + QUEUE_NAME + "' should exist!", q);
    // suspend it
    q.suspend();
    // we start "some" jobs:
    for (int i = 0; i < NUM_JOBS; i++) {
        final String subTopic = TOPIC + "/sub" + (i % 10);
        final Map<String, Object> props = new HashMap<String, Object>();
        if (i < 10) {
            props.put("sleep", 300);
        } else {
            props.put("sleep", 30);
        }
        jobManager.addJob(subTopic, props);
    }
    // start the queue
    q.resume();
    while (count.get() < NUM_JOBS + 1) {
        assertEquals("Failed count", 0, q.getStatistics().getNumberOfFailedJobs());
        assertEquals("Cancelled count", 0, q.getStatistics().getNumberOfCancelledJobs());
        sleep(300);
    }
    // we started one event before the test, so add one
    assertEquals("Finished count", NUM_JOBS + 1, count.get());
    assertEquals("Finished count", NUM_JOBS + 1, jobManager.getStatistics().getNumberOfFinishedJobs());
    assertEquals("Finished count", NUM_JOBS + 1, q.getStatistics().getNumberOfFinishedJobs());
    assertEquals("Failed count", 0, q.getStatistics().getNumberOfFailedJobs());
    assertEquals("Cancelled count", 0, q.getStatistics().getNumberOfCancelledJobs());
    for (int i = 1; i <= MAX_PAR; i++) {
        assertTrue("# Participants " + String.valueOf(i) + " not in " + maxParticipants, maxParticipants.contains(i));
    }
}
Also used : HashMap(java.util.HashMap) EventHandler(org.osgi.service.event.EventHandler) Barrier(org.apache.sling.event.impl.Barrier) JobManager(org.apache.sling.event.jobs.JobManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Event(org.osgi.service.event.Event) JobConsumer(org.apache.sling.event.jobs.consumer.JobConsumer) Job(org.apache.sling.event.jobs.Job) Queue(org.apache.sling.event.jobs.Queue) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Event (org.osgi.service.event.Event)13 EventHandler (org.osgi.service.event.EventHandler)13 Test (org.junit.Test)10 Job (org.apache.sling.event.jobs.Job)9 JobManager (org.apache.sling.event.jobs.JobManager)7 JobConsumer (org.apache.sling.event.jobs.consumer.JobConsumer)7 HashMap (java.util.HashMap)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Barrier (org.apache.sling.event.impl.Barrier)5 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Queue (org.apache.sling.event.jobs.Queue)3 IOException (java.io.IOException)2 Hashtable (java.util.Hashtable)2 JobExecutionContext (org.apache.sling.event.jobs.consumer.JobExecutionContext)2 JobExecutionResult (org.apache.sling.event.jobs.consumer.JobExecutionResult)2 JobExecutor (org.apache.sling.event.jobs.consumer.JobExecutor)2 IResourceChangeEvent (org.eclipse.core.resources.IResourceChangeEvent)2 Bundle (org.osgi.framework.Bundle)2 PersistenceException (org.talend.commons.exception.PersistenceException)2