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 + "*")));
}
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);
}
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();
}
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();
}
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));
}
}
Aggregations