-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update WildflyEJBTimerRetriever.java
- Loading branch information
Showing
8 changed files
with
296 additions
and
41 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
...ervices-ejb-timer/src/main/java/org/jbpm/services/ejb/timer/DefaultEJBTimerRetriever.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2023 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* 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 org.jbpm.services.ejb.timer; | ||
|
||
import java.util.Collection; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.ejb.Timer; | ||
import javax.ejb.TimerService; | ||
|
||
class DefaultEJBTimerRetriever extends EJBTimerRetriever<Void> { | ||
|
||
protected DefaultEJBTimerRetriever(TimerService timerService) { | ||
super(timerService); | ||
} | ||
|
||
@Override | ||
public Collection<Object> getTimers(String jobName, Void accepted) { | ||
return timerService.getTimers().stream().map(Timer::getInfo).collect(Collectors.toList()); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...ces-ejb/jbpm-services-ejb-timer/src/main/java/org/jbpm/services/ejb/timer/EJBTimerDB.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2023 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* 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 org.jbpm.services.ejb.timer; | ||
|
||
enum EJBTimerDB { | ||
ORACLE("select info from jboss_ejb_timer where utl_raw.cast_to_varchar2(utl_encode.base64_decode(utl_raw.cast_to_raw(dbms_lob.substr(info,2000,1)))) like '%?%'"), | ||
POSTGRESQL("select info from jboss_ejb_timerwhere decode(info, 'base64') like '%?%'"); | ||
|
||
private final String query; | ||
|
||
private EJBTimerDB (String query) { | ||
this.query = query; | ||
} | ||
|
||
public String getQuery() { | ||
return query; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
.../jbpm-services-ejb-timer/src/main/java/org/jbpm/services/ejb/timer/EJBTimerRetriever.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2023 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* 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 org.jbpm.services.ejb.timer; | ||
|
||
|
||
import java.util.Collection; | ||
import java.util.Optional; | ||
|
||
import javax.ejb.TimerService; | ||
|
||
import org.jbpm.process.core.timer.impl.GlobalTimerService; | ||
|
||
public abstract class EJBTimerRetriever<T> { | ||
|
||
protected final TimerService timerService; | ||
|
||
protected EJBTimerRetriever (TimerService timerService) { | ||
this.timerService = timerService; | ||
} | ||
|
||
public Optional<T> accept (GlobalTimerService globalTimerService) { | ||
return Optional.empty(); | ||
} | ||
|
||
public abstract Collection<Object> getTimers(String jobName,T accepted); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...bpm-services-ejb-timer/src/main/java/org/jbpm/services/ejb/timer/WildflyAcceptedInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2023 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* 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 org.jbpm.services.ejb.timer; | ||
|
||
import javax.persistence.EntityManagerFactory; | ||
|
||
class WildflyAcceptedInfo { | ||
private final EJBTimerDB db; | ||
private final EntityManagerFactory emf; | ||
|
||
public WildflyAcceptedInfo(EJBTimerDB db, EntityManagerFactory emf) { | ||
this.db = db; | ||
this.emf = emf; | ||
} | ||
public EntityManagerFactory getEmf() { | ||
return emf; | ||
} | ||
|
||
public EJBTimerDB getDb() { | ||
return db; | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
...ervices-ejb-timer/src/main/java/org/jbpm/services/ejb/timer/WildflyEJBTimerRetriever.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright 2023 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* 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 org.jbpm.services.ejb.timer; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.ejb.TimerService; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
|
||
import org.jbpm.process.core.timer.impl.GlobalTimerService; | ||
import org.jbpm.runtime.manager.impl.jpa.EntityManagerFactoryManager; | ||
import org.kie.internal.runtime.manager.InternalRuntimeManager; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
class WildflyEJBTimerRetriever extends EJBTimerRetriever<WildflyAcceptedInfo> { | ||
|
||
private final static Logger logger = LoggerFactory.getLogger(WildflyEJBTimerRetriever.class); | ||
private final boolean isPersistentWildfly; | ||
private Object persistence; | ||
|
||
|
||
protected WildflyEJBTimerRetriever(TimerService timerService) { | ||
super(timerService); | ||
isPersistentWildfly = isPersistentWildfly(); | ||
if (isPersistentWildfly) { | ||
logger.info ("EJBTimer is using Wildfly persistence"); | ||
} | ||
} | ||
|
||
private boolean isPersistentWildfly() { | ||
Class<? extends TimerService> clazz = timerService.getClass(); | ||
if (clazz.getName().equals("org.jboss.as.ejb3.timerservice.TimerServiceImpl")) { | ||
try { | ||
Field field = clazz.getDeclaredField("persistence"); | ||
field.setAccessible(true); | ||
persistence = field.get(timerService); | ||
return persistence != null && persistence.getClass().getName().equals("org.jboss.as.ejb3.timerservice.persistence.database.DatabaseTimerPersistence"); | ||
} catch (ReflectiveOperationException ex) { | ||
logger.trace("Exception retrieving timer service persistence field", ex); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
|
||
@Override | ||
public Optional<WildflyAcceptedInfo> accept(GlobalTimerService globalTimerService) { | ||
return isPersistentWildfly ? getDB(globalTimerService.getRuntimeManager()) : Optional.empty(); | ||
} | ||
|
||
private Optional<WildflyAcceptedInfo> getDB(InternalRuntimeManager runtime) { | ||
EntityManagerFactory emf = EntityManagerFactoryManager.get().getOrCreate(runtime.getDeploymentDescriptor().getPersistenceUnit()); | ||
for (Object value : emf.getProperties().values()) { | ||
if (value instanceof String) { | ||
String search = value.toString().toLowerCase(); | ||
if (search.contains("oracle")) { | ||
return Optional.of(new WildflyAcceptedInfo(EJBTimerDB.ORACLE, emf)); | ||
} else if (search.contains("postgresql")) { | ||
return Optional.of(new WildflyAcceptedInfo(EJBTimerDB.POSTGRESQL,emf)); | ||
} | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public Collection<Object> getTimers(String jobName, WildflyAcceptedInfo accepted) { | ||
EntityManager em = accepted.getEmf().createEntityManager(); | ||
List<String> results = em.createNativeQuery(accepted.getDb().getQuery(), String.class).setParameter(1, jobName).getResultList(); | ||
return results.stream().map(this::deserialize).collect(Collectors.toList()); | ||
} | ||
|
||
private Object deserialize( String info) { | ||
try { | ||
Method method = persistence.getClass().getMethod("deSerialize", String.class); | ||
return method.invoke(persistence, info); | ||
} catch (ReflectiveOperationException e) { | ||
throw new IllegalStateException("Error deserializing info", e); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.