├── .circleci
└── config.yml
├── .gitignore
├── .travis.yml
├── LICENSE
├── NOTICE
├── README.md
├── kernel
├── LICENSE
├── README.md
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── adeptj
│ │ └── runtime
│ │ └── kernel
│ │ ├── AbstractServer.java
│ │ ├── ConfigProvider.java
│ │ ├── Constants.java
│ │ ├── FilterInfo.java
│ │ ├── SciInfo.java
│ │ ├── Server.java
│ │ ├── ServerMode.java
│ │ ├── ServerPostStopTask.java
│ │ ├── ServerRuntime.java
│ │ ├── ServerShutdownHook.java
│ │ ├── ServletDeployment.java
│ │ ├── ServletInfo.java
│ │ ├── UserManager.java
│ │ ├── exception
│ │ ├── RuntimeInitializationException.java
│ │ └── ServerException.java
│ │ ├── osgi
│ │ └── PackageExportsProvider.java
│ │ ├── security
│ │ └── DefaultUserManager.java
│ │ ├── servlet
│ │ └── FaviconServlet.java
│ │ └── util
│ │ ├── ClasspathResource.java
│ │ ├── Configs.java
│ │ ├── Environment.java
│ │ ├── IOUtils.java
│ │ ├── KeyStores.java
│ │ ├── MVStoreUtil.java
│ │ ├── PasswordEncoder.java
│ │ ├── RequestUtil.java
│ │ ├── ResponseUtil.java
│ │ ├── SslContextFactory.java
│ │ └── Times.java
│ └── resources
│ ├── i18n
│ └── kernel.properties
│ └── reference.conf
├── main
├── LICENSE
├── NOTICE
├── README.md
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── adeptj
│ │ │ └── runtime
│ │ │ ├── common
│ │ │ ├── BridgeServletConfigHolder.java
│ │ │ ├── BundleContextHolder.java
│ │ │ ├── Constants.java
│ │ │ ├── LogbackManagerHolder.java
│ │ │ ├── OSGiUtil.java
│ │ │ ├── ServerMode.java
│ │ │ ├── ServletContextHolder.java
│ │ │ └── Servlets.java
│ │ │ ├── core
│ │ │ ├── DefaultStartupAware.java
│ │ │ ├── Launcher.java
│ │ │ ├── LoggerCleanupTask.java
│ │ │ ├── RuntimeInitializer.java
│ │ │ ├── ServerBootstrapper.java
│ │ │ └── StartupAware.java
│ │ │ ├── exception
│ │ │ ├── RuntimeInitializationException.java
│ │ │ └── ServerException.java
│ │ │ ├── html
│ │ │ ├── I18nSupport.java
│ │ │ ├── TemplateEngine.java
│ │ │ ├── TemplateEngineContext.java
│ │ │ └── TemplateProcessingException.java
│ │ │ ├── logging
│ │ │ ├── DebugLevelHighlightingConverter.java
│ │ │ ├── FileAppenderConfig.java
│ │ │ ├── LogbackInitializer.java
│ │ │ ├── LogbackManager.java
│ │ │ └── OSGiLoggerConfig.java
│ │ │ ├── osgi
│ │ │ ├── BridgeHttpSessionIdListener.java
│ │ │ ├── BridgeHttpSessionListener.java
│ │ │ ├── BridgeServiceTracker.java
│ │ │ ├── BridgeServletContextAttributeListener.java
│ │ │ ├── BundleProvisioner.java
│ │ │ ├── DispatcherServletConfig.java
│ │ │ ├── DispatcherServletTracker.java
│ │ │ ├── DispatcherServletWrapper.java
│ │ │ ├── EventDispatcherTracker.java
│ │ │ ├── FrameworkEvents.java
│ │ │ ├── FrameworkLauncher.java
│ │ │ ├── FrameworkLifecycleListener.java
│ │ │ ├── FrameworkManager.java
│ │ │ ├── FrameworkShutdownHandler.java
│ │ │ ├── HttpSessionEvents.java
│ │ │ ├── LoggerConfigFactoryListener.java
│ │ │ └── ServiceTrackers.java
│ │ │ └── servlet
│ │ │ ├── AdminServlet.java
│ │ │ ├── BridgeServlet.java
│ │ │ └── ErrorServlet.java
│ ├── resources
│ │ ├── META-INF
│ │ │ └── services
│ │ │ │ └── ch.qos.logback.classic.spi.Configurator
│ │ ├── application.conf
│ │ ├── banner.txt
│ │ ├── framework.properties
│ │ ├── i18n
│ │ │ └── main.properties
│ │ └── server.p12
│ └── webapp
│ │ ├── i18n
│ │ └── messages.properties
│ │ ├── static
│ │ ├── css
│ │ │ ├── error-page.css
│ │ │ ├── main.css
│ │ │ └── vendors
│ │ │ │ └── bootstrap.min.css
│ │ └── img
│ │ │ ├── favicon.ico
│ │ │ └── logo.png
│ │ └── templates
│ │ ├── change-pwd.html
│ │ ├── error.html
│ │ └── login.html
│ └── test
│ └── java
│ └── com
│ └── adeptj
│ └── runtime
│ └── common
│ └── BundleContextHolderTest.java
├── pom.xml
└── server-adapters
├── jetty
├── LICENSE
├── README.md
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── adeptj
│ │ └── runtime
│ │ └── jetty
│ │ ├── JettyServer.java
│ │ ├── MVStoreLoginService.java
│ │ ├── SecurityConfigurer.java
│ │ ├── Sha256Base64EncodedPassword.java
│ │ ├── handler
│ │ ├── ContextPathHandler.java
│ │ └── HealthCheckHandler.java
│ │ └── osgi
│ │ └── JettyPackageExportsProvider.java
│ └── resources
│ ├── META-INF
│ └── services
│ │ ├── com.adeptj.runtime.kernel.Server
│ │ └── com.adeptj.runtime.kernel.osgi.PackageExportsProvider
│ ├── i18n
│ └── jetty.properties
│ └── reference.conf
├── pom.xml
├── tomcat
├── LICENSE
├── README.md
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── adeptj
│ │ └── runtime
│ │ └── tomcat
│ │ ├── ConnectorConfigurer.java
│ │ ├── GeneralConfigurer.java
│ │ ├── MVStoreCredentialHandler.java
│ │ ├── MVStoreRealm.java
│ │ ├── SecurityConfigurer.java
│ │ ├── ServerConstants.java
│ │ ├── TomcatServer.java
│ │ ├── filter
│ │ └── ContextPathFilter.java
│ │ └── servlet
│ │ └── HealthCheckServlet.java
│ └── resources
│ ├── META-INF
│ └── services
│ │ └── com.adeptj.runtime.kernel.Server
│ ├── i18n
│ └── tomcat.properties
│ └── reference.conf
└── undertow
├── LICENSE
├── README.md
├── pom.xml
└── src
└── main
├── java
└── com
│ └── adeptj
│ └── runtime
│ └── undertow
│ ├── UndertowServer.java
│ ├── core
│ ├── BaseOptions.java
│ ├── ServerConstants.java
│ ├── ServerOptions.java
│ ├── SimpleAccount.java
│ ├── SimpleIdentityManager.java
│ ├── SimplePrincipal.java
│ ├── SocketOptions.java
│ └── WorkerOptions.java
│ ├── handler
│ ├── HealthCheckHandler.java
│ └── ServletInitialHandlerWrapper.java
│ └── predicate
│ └── ContextPathPredicate.java
└── resources
├── META-INF
└── services
│ └── com.adeptj.runtime.kernel.Server
├── i18n
└── undertow.properties
└── reference.conf
/.circleci/config.yml:
--------------------------------------------------------------------------------
1 | # Use the latest 2.1 version of CircleCI pipeline process engine.
2 | # See: https://circleci.com/docs/2.0/configuration-reference
3 | version: 2.1
4 |
5 | # Define a job to be invoked later in a workflow.
6 | # See: https://circleci.com/docs/2.0/configuration-reference/#jobs
7 | jobs:
8 | # Below is the definition of your job to build and test your app, you can rename and customize it as you want.
9 | build-and-test:
10 | # These next lines define a Docker executor: https://circleci.com/docs/2.0/executor-types/
11 | # You can specify an image from Dockerhub or use one of our Convenience Images from CircleCI's Developer Hub.
12 | # Be sure to update the Docker image tag below to openjdk version of your application.
13 | # A list of available CircleCI Docker Convenience Images are available here: https://circleci.com/developer/images/image/cimg/openjdk
14 | docker:
15 | - image: cimg/openjdk:21.0.2
16 | # Add steps to the job
17 | # See: https://circleci.com/docs/2.0/configuration-reference/#steps
18 | steps:
19 | # Checkout the code as the first step.
20 | - checkout
21 | # Use mvn clean and package as the standard maven build phase
22 | - run:
23 | name: Build
24 | command: mvn -B -DskipTests clean package
25 | # Then run your tests!
26 | - run:
27 | name: Test
28 | command: mvn test
29 |
30 | # Invoke jobs via workflows
31 | # See: https://circleci.com/docs/2.0/configuration-reference/#workflows
32 | workflows:
33 | sample: # This is the name of the workflow, feel free to change it to better match your workflow.
34 | # Inside the workflow, you define the jobs you want to run.
35 | jobs:
36 | - build-and-test
37 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 | /target
3 |
4 | # Mobile Tools for Java (J2ME)
5 | .mtj.tmp/
6 |
7 | # Package Files #
8 | *.jar
9 | *.war
10 | *.ear
11 |
12 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
13 | hs_err_pid*
14 | dependency-reduced-pom.xml
15 | deployment/log
16 | .idea
17 | adeptj-runtime.iml
18 | deployment
19 | target_swiftype
20 | *.pem
21 | *.prefs
22 | .classpath
23 | .project
24 | .DS_Store
25 | .factorypath
26 | /.apt_generated/
27 | /.apt_generated_tests/
28 | target
29 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | sudo: false
3 | install: true
4 |
5 | addons:
6 | sonarcloud:
7 | organization: "adeptj"
8 | token:
9 | secure: ${SONAR_TOKEN}
10 |
11 | jdk:
12 | - openjdk11
13 |
14 | script:
15 | - mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent package sonar:sonar -Dsonar.projectKey=adeptj-runtime
16 |
17 | cache:
18 | directories:
19 | - '$HOME/.m2/repository'
20 | - '$HOME/.sonar/cache'
21 |
22 | branches:
23 | only:
24 | - master
--------------------------------------------------------------------------------
/NOTICE:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # #
3 | # Copyright 2016, AdeptJ (http://adeptj.com) #
4 | # #
5 | # Licensed under the Apache License, Version 2.0 (the "License"); #
6 | # you may not use this file except in compliance with the License. #
7 | # You may obtain a copy of the License at #
8 | # #
9 | # http://www.apache.org/licenses/LICENSE-2.0 #
10 | # #
11 | # Unless required by applicable law or agreed to in writing, software #
12 | # distributed under the License is distributed on an "AS IS" BASIS, #
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
14 | # See the License for the specific language governing permissions and #
15 | # limitations under the License. #
16 | # #
17 | ###############################################################################
--------------------------------------------------------------------------------
/kernel/README.md:
--------------------------------------------------------------------------------
1 | # adeptj-runtime-kernel
2 | Core of AdeptJ Runtime
3 |
--------------------------------------------------------------------------------
/kernel/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.adeptj
8 | adeptj-parent
9 | 45
10 |
11 |
12 | adeptj-runtime-kernel
13 | 1.0.0
14 | jar
15 | AdeptJ Runtime :: Kernel
16 | High performance, dynamic, modular runtime for RESTful APIs, MicroServices and WebApps.
17 | https://www.adeptj.com
18 | 2016
19 |
20 |
21 | UTF-8
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 | org.apache.maven.plugins
30 | maven-compiler-plugin
31 |
32 |
33 |
34 | org.apache.maven.plugins
35 | maven-surefire-plugin
36 |
37 | ${skipTests}
38 |
39 |
40 |
41 |
42 | org.apache.maven.plugins
43 | maven-enforcer-plugin
44 |
45 |
46 |
47 | org.codehaus.mojo
48 | versions-maven-plugin
49 |
50 | false
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 | org.apache.commons
62 | commons-lang3
63 |
64 |
65 |
66 | com.h2database
67 | h2-mvstore
68 |
69 |
70 |
71 | com.typesafe
72 | config
73 |
74 |
75 |
76 | org.slf4j
77 | slf4j-api
78 |
79 |
80 |
81 | jakarta.servlet
82 | jakarta.servlet-api
83 |
84 |
85 |
86 |
87 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/FilterInfo.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel;
21 |
22 | import jakarta.servlet.Filter;
23 |
24 | /**
25 | * {@link Filter} info object needed for deployment on AdeptJ Runtime.
26 | *
27 | * @author Rakesh Kumar, AdeptJ
28 | */
29 | public class FilterInfo {
30 |
31 | private final String filterName;
32 |
33 | private final String pattern;
34 |
35 | private Class extends Filter> filterClass;
36 |
37 | private Filter filterInstance;
38 |
39 | public FilterInfo(String filterName, String pattern) {
40 | this.filterName = filterName;
41 | this.pattern = pattern;
42 | }
43 |
44 | public String getFilterName() {
45 | return filterName;
46 | }
47 |
48 | public String getPattern() {
49 | return pattern;
50 | }
51 |
52 | public Class extends Filter> getFilterClass() {
53 | return filterClass;
54 | }
55 |
56 | public void setFilterClass(Class extends Filter> filterClass) {
57 | this.filterClass = filterClass;
58 | }
59 |
60 | public Filter getFilterInstance() {
61 | return filterInstance;
62 | }
63 |
64 | public void setFilterInstance(Filter filterInstance) {
65 | this.filterInstance = filterInstance;
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/SciInfo.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel;
21 |
22 | import jakarta.servlet.ServletContainerInitializer;
23 | import java.util.Set;
24 |
25 | /**
26 | * {@link ServletContainerInitializer} info object needed for deployment on AdeptJ Runtime.
27 | *
28 | * @author Rakesh Kumar, AdeptJ
29 | */
30 | public class SciInfo {
31 |
32 | private ServletContainerInitializer sci;
33 |
34 | private final Set> handleTypes;
35 |
36 | private Class extends ServletContainerInitializer> sciClass;
37 |
38 | // Tomcat and Jetty expects an instance of ServletContainerInitializer.
39 | public SciInfo(ServletContainerInitializer sci, Set> handleTypes) {
40 | this.sci = sci;
41 | this.handleTypes = handleTypes;
42 | }
43 |
44 | // Undertow expects ServletContainerInitializer class.
45 | public SciInfo(Class extends ServletContainerInitializer> sciClass, Set> handleTypes) {
46 | this.sciClass = sciClass;
47 | this.handleTypes = handleTypes;
48 | }
49 |
50 | public ServletContainerInitializer getSciInstance() {
51 | return sci;
52 | }
53 |
54 | public Class extends ServletContainerInitializer> getSciClass() {
55 | return sciClass;
56 | }
57 |
58 | public Set> getHandleTypes() {
59 | return handleTypes;
60 | }
61 |
62 | public Class>[] getHandleTypesArray() {
63 | return this.handleTypes.toArray(new Class[0]);
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/Server.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel;
21 |
22 | import com.typesafe.config.Config;
23 | import jakarta.servlet.ServletContainerInitializer;
24 |
25 | import java.util.List;
26 |
27 | /**
28 | * Server interface to be implemented by a given server adapter such as Tomcat, Jetty, Undertow etc.
29 | *
30 | * @author Rakesh Kumar, AdeptJ
31 | */
32 | public interface Server {
33 |
34 | ServerRuntime getRuntime();
35 |
36 | /**
37 | * Starts the given {@link Server} instance.
38 | *
39 | * @param deployment Servlet deployment information for (Tomcat or Jetty or Undertow) instance.
40 | * @param appConfig the application configuration.
41 | * @param args the program arguments to the server instance.
42 | */
43 | void start(ServletDeployment deployment, Config appConfig, String[] args) throws Exception;
44 |
45 | default void postStart() {
46 | // NOOP
47 | }
48 |
49 | void stop();
50 |
51 | default void registerServlets(List servletInfos) {
52 | }
53 |
54 | default void registerFilters(List filterInfos) {
55 | }
56 |
57 | default void registerErrorPages(List errorCodes) {
58 | }
59 |
60 | void addServletContextAttribute(String name, Object value);
61 |
62 | UserManager getUserManager();
63 | }
64 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/ServerMode.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel;
21 |
22 | /**
23 | * Execution mode for optimizing server settings according to the mode provided.
24 | *
25 | * Note: Defaults to DEV mode.
26 | *
27 | * @author Rakesh.Kumar, AdeptJ
28 | */
29 | public enum ServerMode {
30 |
31 | DEV,
32 |
33 | PROD
34 | }
35 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/ServerPostStopTask.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel;
21 |
22 | /**
23 | * A task that needs to be executed while server is stopping.
24 | *
25 | * @author Rakesh Kumar, AdeptJ
26 | */
27 | public interface ServerPostStopTask {
28 |
29 | void execute();
30 | }
31 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/ServerRuntime.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel;
21 |
22 | /**
23 | * Enum denoting the Server adapters supported by AdeptJ Runtime.
24 | *
25 | * @author Rakesh Kumar, AdeptJ
26 | */
27 | public enum ServerRuntime {
28 |
29 | JETTY("Jetty"),
30 |
31 | TOMCAT("Tomcat"),
32 |
33 | UNDERTOW("Undertow");
34 |
35 | private final String name;
36 |
37 | ServerRuntime(String name) {
38 | this.name = name;
39 | }
40 |
41 | public String getLowerCaseName() {
42 | return this.name.toLowerCase();
43 | }
44 |
45 | @Override
46 | public String toString() {
47 | return this.name;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/ServerShutdownHook.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel;
21 |
22 | /**
23 | * A shutdown hook which stops the underlying server powering AdeptJ Runtime.
24 | *
25 | * @author Rakesh Kumar, AdeptJ
26 | */
27 | public final class ServerShutdownHook extends Thread {
28 |
29 | private final Server server;
30 |
31 | public ServerShutdownHook(Server server, String threadName) {
32 | super(threadName);
33 | this.server = server;
34 | }
35 |
36 | /**
37 | * Calls the {@link Server#stop()} in run method.
38 | */
39 | @Override
40 | public void run() {
41 | this.server.stop();
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/ServletDeployment.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel;
21 |
22 | import java.util.ArrayList;
23 | import java.util.List;
24 |
25 | /**
26 | * Deployment info object composing the {@link SciInfo}, Servlets and Filters needed to be deployed bu AdeptJ Runtime.
27 | *
28 | * @author Rakesh Kumar, AdeptJ
29 | */
30 | public class ServletDeployment {
31 |
32 | private final SciInfo sciInfo;
33 |
34 | // Servlet
35 | private final List servletInfos;
36 |
37 | // Filter
38 | private final List filterInfos;
39 |
40 | public ServletDeployment(SciInfo sciInfo) {
41 | this.sciInfo = sciInfo;
42 | this.servletInfos = new ArrayList<>();
43 | this.filterInfos = new ArrayList<>();
44 | }
45 |
46 | public SciInfo getSciInfo() {
47 | return sciInfo;
48 | }
49 |
50 | public List getServletInfos() {
51 | return servletInfos;
52 | }
53 |
54 | public ServletDeployment addServletInfo(ServletInfo info) {
55 | this.servletInfos.add(info);
56 | return this;
57 | }
58 |
59 | public List getFilterInfos() {
60 | return filterInfos;
61 | }
62 |
63 | public ServletDeployment addFilterInfo(FilterInfo info) {
64 | this.filterInfos.add(info);
65 | return this;
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/ServletInfo.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel;
21 |
22 | import jakarta.servlet.http.HttpServlet;
23 |
24 | /**
25 | * {@link HttpServlet} info object needed for deployment on AdeptJ Runtime.
26 | *
27 | * @author Rakesh Kumar, AdeptJ
28 | */
29 | public record ServletInfo(String servletName, String path, Class extends HttpServlet> servletClass) {
30 | }
31 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/UserManager.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel;
21 |
22 | import java.util.List;
23 |
24 | /**
25 | * UserManager interface for handling user authentication on AdeptJ Runtime.
26 | *
27 | * @author Rakesh Kumar, AdeptJ
28 | */
29 | public interface UserManager {
30 |
31 | String getPassword(String username);
32 |
33 | boolean matchPassword(String inputPassword, String storedPassword);
34 |
35 | boolean matchPassword(char[] inputPassword, char[] storedPassword);
36 |
37 | String encodePassword(String password);
38 |
39 | List getRoles(String username);
40 | }
41 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/exception/RuntimeInitializationException.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel.exception;
21 |
22 | import java.io.Serial;
23 |
24 | /**
25 | * RuntimeInitializationException: Exception thrown by AdeptJ Runtime initialization code.
26 | *
27 | * @author Rakesh.Kumar, AdeptJ
28 | */
29 | public class RuntimeInitializationException extends RuntimeException {
30 |
31 | @Serial
32 | private static final long serialVersionUID = 6707002227136114960L;
33 |
34 | public RuntimeInitializationException(Throwable cause) {
35 | super(cause);
36 | }
37 | }
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/exception/ServerException.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel.exception;
21 |
22 | import java.io.Serial;
23 |
24 | /**
25 | * ServerException: Exception thrown by AdeptJ Runtime.
26 | *
27 | * @author Rakesh.Kumar, AdeptJ
28 | */
29 | public class ServerException extends RuntimeException {
30 |
31 | @Serial
32 | private static final long serialVersionUID = -1812814226481606887L;
33 |
34 | public ServerException(Throwable cause) {
35 | super(cause);
36 | }
37 | }
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/osgi/PackageExportsProvider.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel.osgi;
21 |
22 | /**
23 | * SPI for providing OSGi export packages.
24 | *
25 | * @author Rakesh Kumar, AdeptJ
26 | */
27 | public interface PackageExportsProvider {
28 |
29 | default String getName() {
30 | return this.getClass().getName();
31 | }
32 |
33 | /**
34 | * Gets the java packages to be exported by OSGi from system bundle.
35 | *
36 | * @return comma separated packages with version number, if any.
37 | */
38 | String getPackageExports();
39 | }
40 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/servlet/FaviconServlet.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel.servlet;
21 |
22 | import jakarta.servlet.ServletException;
23 | import jakarta.servlet.http.HttpServlet;
24 | import jakarta.servlet.http.HttpServletRequest;
25 | import jakarta.servlet.http.HttpServletResponse;
26 |
27 | import java.io.IOException;
28 | import java.io.Serial;
29 |
30 | /**
31 | * {@link HttpServlet} implementation for serving Favicon.
32 | *
33 | * @author Rakesh Kumar, AdeptJ
34 | */
35 | public class FaviconServlet extends HttpServlet {
36 |
37 | @Serial
38 | private static final long serialVersionUID = -4158958392097965401L;
39 |
40 | private static final String CONTENT_TYPE = "image/x-icon";
41 |
42 | private static final String FAVICON_PATH = "/static/img/favicon.ico";
43 |
44 | @Override
45 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
46 | resp.setContentType(CONTENT_TYPE);
47 | req.getRequestDispatcher(FAVICON_PATH).include(req, resp);
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/util/ClasspathResource.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel.util;
21 |
22 | import java.io.IOException;
23 | import java.net.URL;
24 | import java.util.Enumeration;
25 |
26 | /**
27 | * Utility for accessing classpath resources such as files etc.
28 | *
29 | * @author Rakesh Kumar, AdeptJ
30 | */
31 | public class ClasspathResource {
32 |
33 | public static URL toUrl(String resourceName, ClassLoader cl) {
34 | return cl.getResource(resourceName);
35 | }
36 |
37 | public static Enumeration toUrls(String resourceName, ClassLoader cl) throws IOException {
38 | return cl.getResources(resourceName);
39 | }
40 |
41 | private ClasspathResource() {
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/util/Configs.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel.util;
21 |
22 | import com.typesafe.config.Config;
23 | import org.apache.commons.lang3.StringUtils;
24 |
25 | import java.util.Collections;
26 | import java.util.List;
27 |
28 | /**
29 | * Utility methods for typesafe {@link Config} object.
30 | *
31 | * @author Rakesh Kumar, AdeptJ
32 | */
33 | public class Configs {
34 |
35 | /**
36 | * Checks whether a value is present and non-null at the given path.
37 | *
38 | * @return true or false basis a value is present and non-null at the given path.
39 | */
40 | public static boolean isPresent(Config config, String path) {
41 | if (config == null || StringUtils.isEmpty(path)) {
42 | return false;
43 | }
44 | return config.hasPath(path);
45 | }
46 |
47 | public static List getStringList(Config config, String path) {
48 | if (isPresent(config, path)) {
49 | return config.getStringList(path);
50 | }
51 | return Collections.emptyList();
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/util/Environment.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel.util;
21 |
22 | import com.adeptj.runtime.kernel.ServerMode;
23 |
24 | import java.nio.file.Path;
25 |
26 | import static com.adeptj.runtime.kernel.Constants.CONF_DIR;
27 | import static com.adeptj.runtime.kernel.Constants.DIR_ADEPTJ_RUNTIME;
28 | import static com.adeptj.runtime.kernel.Constants.DIR_DEPLOYMENT;
29 | import static com.adeptj.runtime.kernel.Constants.FRAMEWORK_CONF_FILE;
30 | import static com.adeptj.runtime.kernel.Constants.SYS_PROP_SERVER_MODE;
31 | import static org.apache.commons.lang3.SystemUtils.USER_DIR;
32 |
33 | /**
34 | * Utility methods for getting environment details AdeptJ Runtime is running in.
35 | *
36 | * @author Rakesh.Kumar, AdeptJ.
37 | */
38 | public final class Environment {
39 |
40 | /**
41 | * Deny direct instantiation.
42 | */
43 | private Environment() {
44 | }
45 |
46 | public static boolean isDev() {
47 | return ServerMode.DEV.toString().equalsIgnoreCase(System.getProperty(SYS_PROP_SERVER_MODE));
48 | }
49 |
50 | public static Path getConfDirPath() {
51 | return Path.of(USER_DIR, DIR_ADEPTJ_RUNTIME, DIR_DEPLOYMENT, CONF_DIR);
52 | }
53 |
54 | public static Path getFrameworkConfPath() {
55 | return Path.of(USER_DIR, DIR_ADEPTJ_RUNTIME, DIR_DEPLOYMENT, FRAMEWORK_CONF_FILE);
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/util/IOUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel.util;
21 |
22 | import java.io.ByteArrayOutputStream;
23 | import java.io.IOException;
24 | import java.io.InputStream;
25 |
26 | import static java.nio.charset.StandardCharsets.UTF_8;
27 |
28 | /**
29 | * Common utility methods.
30 | *
31 | * @author Rakesh.Kumar, AdeptJ
32 | */
33 | public final class IOUtils {
34 |
35 | /**
36 | * Deny direct instantiation.
37 | */
38 | private IOUtils() {
39 | }
40 |
41 | public static String toString(InputStream input) throws IOException {
42 | return toByteArrayOutputStream(input).toString(UTF_8);
43 | }
44 |
45 | public static byte[] toBytes(InputStream input) throws IOException {
46 | return toByteArrayOutputStream(input).toByteArray();
47 | }
48 |
49 | private static ByteArrayOutputStream toByteArrayOutputStream(InputStream source) throws IOException {
50 | ByteArrayOutputStream out = new ByteArrayOutputStream();
51 | // Below section is borrowed (with much appreciation) from JBoss Xnio Streams.copyStream :)
52 | final byte[] buffer = new byte[8192];
53 | int res;
54 | for (; ; ) {
55 | res = source.read(buffer);
56 | if (res == -1) {
57 | return out;
58 | }
59 | out.write(buffer, 0, res);
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/util/KeyStores.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel.util;
21 |
22 | import com.adeptj.runtime.kernel.exception.RuntimeInitializationException;
23 |
24 | import java.io.IOException;
25 | import java.io.InputStream;
26 | import java.nio.file.Files;
27 | import java.nio.file.Paths;
28 | import java.security.GeneralSecurityException;
29 | import java.security.KeyStore;
30 |
31 | /**
32 | * Utilities for Java KeyStore.
33 | *
34 | * @author Rakesh.Kumar, AdeptJ
35 | */
36 | final class KeyStores {
37 |
38 | private KeyStores() {
39 | }
40 |
41 | static KeyStore getKeyStore(boolean p12FileExternal, String type, String p12Loc, char[] p12Pwd) {
42 | try (InputStream is = p12FileExternal
43 | ? Files.newInputStream(Paths.get(p12Loc)) : KeyStores.class.getResourceAsStream(p12Loc)) {
44 | KeyStore keyStore = KeyStore.getInstance(type);
45 | keyStore.load(is, p12Pwd);
46 | return keyStore;
47 | } catch (IOException | GeneralSecurityException ex) {
48 | throw new RuntimeInitializationException(ex);
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/util/MVStoreUtil.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel.util;
21 |
22 | import org.h2.mvstore.MVMap;
23 | import org.h2.mvstore.MVStore;
24 |
25 | import static com.adeptj.runtime.kernel.Constants.H2_MAP_ADMIN_CREDENTIALS;
26 | import static com.adeptj.runtime.kernel.Constants.MV_CREDENTIALS_STORE;
27 |
28 | /**
29 | * Utility for MV Store.
30 | *
31 | * @author Rakesh Kumar, AdeptJ
32 | */
33 | public final class MVStoreUtil {
34 |
35 | private MVStoreUtil() {
36 | }
37 |
38 | public static String getValue(String storeName, String mapName, String key) {
39 | try (MVStore store = MVStore.open(storeName)) {
40 | MVMap mvMap = store.openMap(mapName);
41 | return mvMap.get(key);
42 | }
43 | }
44 |
45 | public static String getPassword(String username) {
46 | return getValue(MV_CREDENTIALS_STORE, H2_MAP_ADMIN_CREDENTIALS, username);
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/util/PasswordEncoder.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel.util;
21 |
22 | import java.security.MessageDigest;
23 | import java.security.NoSuchAlgorithmException;
24 | import java.util.Base64;
25 |
26 | import static com.adeptj.runtime.kernel.Constants.SHA_256;
27 | import static java.nio.charset.StandardCharsets.UTF_8;
28 |
29 | /**
30 | * Password encoding utility.
31 | *
32 | * @author Rakesh Kumar, AdeptJ
33 | */
34 | public class PasswordEncoder {
35 |
36 | public static byte[] encodePassword(byte[] password) {
37 | return Base64.getEncoder().encode(sha256(password));
38 | }
39 |
40 | public static byte[] encodePassword(String password) {
41 | return Base64.getEncoder().encode(sha256(password.getBytes(UTF_8)));
42 | }
43 |
44 | private static byte[] sha256(byte[] inputPwdBytes) {
45 | try {
46 | return MessageDigest.getInstance(SHA_256).digest(inputPwdBytes);
47 | } catch (final NoSuchAlgorithmException ex) {
48 | throw new IllegalArgumentException(ex);
49 | }
50 | }
51 |
52 | private PasswordEncoder() {
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/util/RequestUtil.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel.util;
21 |
22 | import com.adeptj.runtime.kernel.exception.ServerException;
23 | import jakarta.servlet.ServletException;
24 | import jakarta.servlet.http.HttpServletRequest;
25 | import org.apache.commons.lang3.exception.ExceptionUtils;
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | import static jakarta.servlet.RequestDispatcher.ERROR_EXCEPTION;
30 |
31 | /**
32 | * Utils for {@link HttpServletRequest}
33 | *
34 | * @author Rakesh.Kumar, AdeptJ
35 | */
36 | public final class RequestUtil {
37 |
38 | private static final Logger LOGGER = LoggerFactory.getLogger(RequestUtil.class);
39 |
40 | private RequestUtil() {
41 | }
42 |
43 | public static Object getAttribute(HttpServletRequest req, String name) {
44 | return req.getAttribute(name);
45 | }
46 |
47 | public static boolean hasException(HttpServletRequest req) {
48 | return getAttribute(req, ERROR_EXCEPTION) != null;
49 | }
50 |
51 | public static String getException(HttpServletRequest req) {
52 | return ExceptionUtils.getStackTrace((Throwable) RequestUtil.getAttribute(req, ERROR_EXCEPTION));
53 | }
54 |
55 | public static void logout(HttpServletRequest req) {
56 | try {
57 | req.logout();
58 | } catch (ServletException ex) {
59 | LOGGER.error(ex.getMessage(), ex);
60 | throw new ServerException(ex);
61 | }
62 | }
63 |
64 | public static boolean isContextRootRequest(HttpServletRequest req) {
65 | return isContextRootRequest(req.getRequestURI());
66 | }
67 |
68 | public static boolean isContextRootRequest(String requestPath) {
69 | return requestPath.equals("/") || requestPath.startsWith("/;jsessionid");
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/util/SslContextFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel.util;
21 |
22 | import com.typesafe.config.Config;
23 |
24 | import javax.net.ssl.KeyManagerFactory;
25 | import javax.net.ssl.SSLContext;
26 | import java.security.GeneralSecurityException;
27 | import java.security.KeyStore;
28 |
29 | import static com.adeptj.runtime.kernel.Constants.KEY_KEYSTORE_TYPE;
30 | import static com.adeptj.runtime.kernel.Constants.KEY_P12_FILE_LOCATION;
31 | import static com.adeptj.runtime.kernel.Constants.KEY_P12_PASSWORD;
32 | import static com.adeptj.runtime.kernel.Constants.KEY_TLS_VERSION;
33 | import static com.adeptj.runtime.kernel.Constants.SYS_PROP_P12_FILE_EXTERNAL;
34 | import static com.adeptj.runtime.kernel.Constants.SYS_PROP_P12_FILE_LOCATION;
35 | import static com.adeptj.runtime.kernel.Constants.SYS_PROP_P12_PASSWORD;
36 | import static com.adeptj.runtime.kernel.Constants.SYS_PROP_TLS_VERSION;
37 |
38 | /**
39 | * Utilities for SSL/TLS.
40 | *
41 | * @author Rakesh.Kumar, AdeptJ
42 | */
43 | public final class SslContextFactory {
44 |
45 | private SslContextFactory() {
46 | }
47 |
48 | public static SSLContext newSslContext(Config httpsConf) throws GeneralSecurityException {
49 | String p12Loc;
50 | char[] p12Pwd;
51 | boolean p12FileExternal = Boolean.getBoolean(SYS_PROP_P12_FILE_EXTERNAL);
52 | if (p12FileExternal) {
53 | p12Loc = System.getProperty(SYS_PROP_P12_FILE_LOCATION);
54 | p12Pwd = System.getProperty(SYS_PROP_P12_PASSWORD).toCharArray();
55 | } else {
56 | p12Loc = httpsConf.getString(KEY_P12_FILE_LOCATION);
57 | p12Pwd = httpsConf.getString(KEY_P12_PASSWORD).toCharArray();
58 | }
59 | String keyStoreType = httpsConf.getString(KEY_KEYSTORE_TYPE);
60 | KeyStore keyStore = KeyStores.getKeyStore(p12FileExternal, keyStoreType, p12Loc, p12Pwd);
61 | KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
62 | kmf.init(keyStore, p12Pwd);
63 | String protocol = System.getProperty(SYS_PROP_TLS_VERSION, httpsConf.getString(KEY_TLS_VERSION));
64 | SSLContext sslContext = SSLContext.getInstance(protocol);
65 | // tm is initialized by SSLContext impl, that's why passing a null.
66 | sslContext.init(kmf.getKeyManagers(), null, null);
67 | return sslContext;
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/kernel/src/main/java/com/adeptj/runtime/kernel/util/Times.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.kernel.util;
21 |
22 | import static java.util.concurrent.TimeUnit.MILLISECONDS;
23 | import static java.util.concurrent.TimeUnit.NANOSECONDS;
24 |
25 | /**
26 | * Utility for providing execution time in different {@link java.util.concurrent.TimeUnit}.
27 | *
28 | * @author Rakesh.Kumar, AdeptJ
29 | */
30 | public final class Times {
31 |
32 | // No instances, just utility methods.
33 | private Times() {
34 | }
35 |
36 | /**
37 | * Returns elapsed time in milliseconds from the provided time in nanoseconds.
38 | *
39 | * @param startTime time in nanoseconds
40 | * @return elapsed time in milliseconds
41 | */
42 | public static long elapsedMillis(final long startTime) {
43 | return NANOSECONDS.toMillis(System.nanoTime() - startTime);
44 | }
45 |
46 | /**
47 | * Returns elapsed time in seconds from the provided time in milliseconds.
48 | *
49 | * @param startTime time in milliseconds
50 | * @return elapsed time in seconds
51 | */
52 | public static long elapsedSeconds(final long startTime) {
53 | return MILLISECONDS.toSeconds(System.currentTimeMillis() - startTime);
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/kernel/src/main/resources/i18n/kernel.properties:
--------------------------------------------------------------------------------
1 | ajrt.port.system.property.not.specified=No port specified via system property: [adeptj.rt.port], will resolve port from configs!
--------------------------------------------------------------------------------
/kernel/src/main/resources/reference.conf:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # #
3 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
4 | # #
5 | # Licensed under the Apache License, Version 2.0 (the "License"); #
6 | # you may not use this file except in compliance with the License. #
7 | # You may obtain a copy of the License at #
8 | # #
9 | # http://www.apache.org/licenses/LICENSE-2.0 #
10 | # #
11 | # Unless required by applicable law or agreed to in writing, software #
12 | # distributed under the License is distributed on an "AS IS" BASIS, #
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
14 | # See the License for the specific language governing permissions and #
15 | # limitations under the License. #
16 | # #
17 | ###############################################################################
18 |
19 | kernel: {
20 | server.port: 8080
21 | server.context.path: ""
22 | }
--------------------------------------------------------------------------------
/main/NOTICE:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # #
3 | # Copyright 2016, AdeptJ (http://adeptj.com) #
4 | # #
5 | # Licensed under the Apache License, Version 2.0 (the "License"); #
6 | # you may not use this file except in compliance with the License. #
7 | # You may obtain a copy of the License at #
8 | # #
9 | # http://www.apache.org/licenses/LICENSE-2.0 #
10 | # #
11 | # Unless required by applicable law or agreed to in writing, software #
12 | # distributed under the License is distributed on an "AS IS" BASIS, #
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
14 | # See the License for the specific language governing permissions and #
15 | # limitations under the License. #
16 | # #
17 | ###############################################################################
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/common/BridgeServletConfigHolder.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.common;
22 |
23 | import jakarta.servlet.ServletConfig;
24 |
25 | /**
26 | * Maintains the BridgeServlet's {@link ServletConfig} instance.
27 | *
28 | * @author Rakesh.Kumar, AdeptJ
29 | */
30 | public enum BridgeServletConfigHolder {
31 |
32 | INSTANCE;
33 |
34 | private ServletConfig bridgeServletConfig;
35 |
36 | public void setBridgeServletConfig(ServletConfig bridgeServletConfig) { // NOSONAR
37 | if (this.bridgeServletConfig == null) {
38 | this.bridgeServletConfig = bridgeServletConfig;
39 | }
40 | }
41 |
42 | public ServletConfig getBridgeServletConfig() {
43 | return bridgeServletConfig;
44 | }
45 |
46 | public static BridgeServletConfigHolder getInstance() {
47 | return INSTANCE;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/common/BundleContextHolder.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.common;
22 |
23 | import org.osgi.framework.BundleContext;
24 |
25 | /**
26 | * This Enum provides the access to the OSGi System Bundle {@link BundleContext}.
27 | *
28 | * @author Rakesh.Kumar, AdeptJ
29 | */
30 | public enum BundleContextHolder {
31 |
32 | INSTANCE;
33 |
34 | private BundleContext bundleContext;
35 |
36 | public BundleContext getBundleContext() {
37 | return this.bundleContext;
38 | }
39 |
40 | public void setBundleContext(BundleContext bundleContext) { // NOSONAR
41 | this.bundleContext = bundleContext;
42 | }
43 |
44 | public static BundleContextHolder getInstance() {
45 | return INSTANCE;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/common/Constants.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.common;
22 |
23 | /**
24 | * Constants, common constants for AdeptJ Runtime.
25 | *
26 | * @author Rakesh.Kumar, AdeptJ
27 | */
28 | public final class Constants {
29 |
30 | public static final String BUNDLES_ROOT_DIR_KEY = "bundles-root-dir";
31 |
32 | public static final String ATTRIBUTE_BUNDLE_CONTEXT = "org.osgi.framework.BundleContext";
33 |
34 | public static final String PEBBLE_CONF_SECTION = "pebble";
35 |
36 | public static final String FELIX_CONF_SECTION = "felix";
37 |
38 | public static final String LOGGING_CONF_SECTION = "logging";
39 |
40 | public static final String VAR_ERROR_CODE = "errorCode";
41 |
42 | public static final String ADMIN_LOGOUT_URI = "/admin/logout";
43 |
44 | public static final String ADMIN_SERVLET_NAME = "AdeptJ AdminServlet";
45 |
46 | public static final String ADMIN_SERVLET_URI = "/admin/*";
47 |
48 | public static final String ERROR_SERVLET_URI = "/ErrorHandler";
49 |
50 | public static final String ERROR_SERVLET_NAME = "AdeptJ ErrorServlet";
51 |
52 | public static final String LOGBACK_VIEW_SERVLET_NAME = "Logback ViewStatusMessagesServlet";
53 |
54 | public static final String LOGBACK_VIEW_SERVLET_URI = "/admin/logback-status";
55 |
56 | public static final String FAVICON_SERVLET_NAME = "AdeptJ FaviconServlet";
57 |
58 | public static final String FAVICON_SERVLET_URI = "/favicon.ico";
59 |
60 | public static final String BANNER_TXT = "/banner.txt";
61 |
62 | public static final String CONTENT_TYPE_HTML_UTF8 = "text/html;charset=UTF-8";
63 |
64 | public static final String OSGI_ADMIN_ROLE = "OSGiAdmin";
65 |
66 | public static final String MV_CREDENTIALS_STORE = "credentials.dat";
67 |
68 | public static final String H2_MAP_ADMIN_CREDENTIALS = "adminCredentials";
69 |
70 | /**
71 | * Deny direct instantiation.
72 | */
73 | private Constants() {
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/common/LogbackManagerHolder.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.common;
22 |
23 | import com.adeptj.runtime.logging.LogbackManager;
24 |
25 | /**
26 | * This Enum provides the access to the {@link LogbackManager}.
27 | *
28 | * @author Rakesh.Kumar, AdeptJ
29 | */
30 | public enum LogbackManagerHolder {
31 |
32 | INSTANCE;
33 |
34 | private LogbackManager logbackManager;
35 |
36 | public void setLogbackManager(LogbackManager logbackManager) {
37 | if (this.logbackManager == null) {
38 | this.logbackManager = logbackManager;
39 | }
40 | }
41 |
42 | public LogbackManager getLogbackManager() {
43 | return this.logbackManager;
44 | }
45 |
46 | public static LogbackManagerHolder getInstance() {
47 | return INSTANCE;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/common/ServerMode.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://wwwadeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.common;
22 |
23 | /**
24 | * Execution mode for optimizing server settings according to the mode provided.
25 | *
26 | * Note: Defaults to DEV mode.
27 | *
28 | * @author Rakesh.Kumar, AdeptJ
29 | */
30 | public enum ServerMode {
31 |
32 | DEV,
33 |
34 | PROD
35 | }
36 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/common/ServletContextHolder.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.common;
22 |
23 | import jakarta.servlet.ServletContext;
24 |
25 | /**
26 | * This Enum provides the access to the {@link ServletContext} and corresponding attributes.
27 | *
28 | * @author Rakesh.Kumar, AdeptJ
29 | */
30 | public enum ServletContextHolder {
31 |
32 | INSTANCE;
33 |
34 | private ServletContext context;
35 |
36 | public void setServletContext(ServletContext context) { // NOSONAR
37 | if (this.context == null) {
38 | this.context = context;
39 | }
40 | }
41 |
42 | public ServletContext getServletContext() {
43 | return this.context;
44 | }
45 |
46 | public T getContextAttributeOfType(String name, Class type) {
47 | final Object value = this.context.getAttribute(name);
48 | return type.isInstance(value) ? type.cast(value) : null;
49 | }
50 |
51 | public void setContextAttribute(String name, Object value) {
52 | this.context.setAttribute(name, value);
53 | }
54 |
55 | public static ServletContextHolder getInstance() {
56 | return INSTANCE;
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/core/DefaultStartupAware.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.core;
22 |
23 | import com.adeptj.runtime.common.Servlets;
24 | import org.slf4j.Logger;
25 | import org.slf4j.LoggerFactory;
26 |
27 | import jakarta.servlet.ServletContext;
28 |
29 | /**
30 | * DefaultStartupAware is a {@link StartupAware} that registers the bridge listeners and servlet.
31 | *
32 | * @author Rakesh.Kumar, AdeptJ
33 | */
34 | public class DefaultStartupAware implements StartupAware {
35 |
36 | /**
37 | * {@inheritDoc}
38 | */
39 | @Override
40 | public void onStartup(ServletContext servletContext) {
41 | Logger logger = LoggerFactory.getLogger(DefaultStartupAware.class);
42 | Servlets.registerBridgeListeners(servletContext);
43 | logger.info("OSGi bridge listeners registered successfully!!");
44 | Servlets.registerBridgeServlet(servletContext);
45 | logger.info("BridgeServlet registered successfully!!");
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/core/LoggerCleanupTask.java:
--------------------------------------------------------------------------------
1 | package com.adeptj.runtime.core;
2 |
3 | import com.adeptj.runtime.common.LogbackManagerHolder;
4 | import com.adeptj.runtime.kernel.ServerPostStopTask;
5 |
6 | public class LoggerCleanupTask implements ServerPostStopTask {
7 |
8 | @Override
9 | public void execute() {
10 | LogbackManagerHolder.getInstance().getLogbackManager().cleanup();
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/core/ServerBootstrapper.java:
--------------------------------------------------------------------------------
1 | package com.adeptj.runtime.core;
2 |
3 | import ch.qos.logback.classic.ViewStatusMessagesServlet;
4 | import com.adeptj.runtime.kernel.SciInfo;
5 | import com.adeptj.runtime.kernel.Server;
6 | import com.adeptj.runtime.kernel.ServletDeployment;
7 | import com.adeptj.runtime.kernel.ServletInfo;
8 | import com.adeptj.runtime.kernel.servlet.FaviconServlet;
9 | import com.adeptj.runtime.osgi.FrameworkLauncher;
10 | import com.adeptj.runtime.servlet.AdminServlet;
11 | import com.adeptj.runtime.servlet.ErrorServlet;
12 | import com.typesafe.config.Config;
13 |
14 | import java.util.LinkedHashSet;
15 | import java.util.Set;
16 |
17 | import static com.adeptj.runtime.common.Constants.ADMIN_SERVLET_NAME;
18 | import static com.adeptj.runtime.common.Constants.ADMIN_SERVLET_URI;
19 | import static com.adeptj.runtime.common.Constants.ERROR_SERVLET_NAME;
20 | import static com.adeptj.runtime.common.Constants.ERROR_SERVLET_URI;
21 | import static com.adeptj.runtime.common.Constants.FAVICON_SERVLET_NAME;
22 | import static com.adeptj.runtime.common.Constants.FAVICON_SERVLET_URI;
23 | import static com.adeptj.runtime.common.Constants.LOGBACK_VIEW_SERVLET_NAME;
24 | import static com.adeptj.runtime.common.Constants.LOGBACK_VIEW_SERVLET_URI;
25 | import static com.adeptj.runtime.kernel.ServerRuntime.UNDERTOW;
26 |
27 | /**
28 | * Bootstrap the given {@link Server} instance.
29 | *
30 | * @author Rakesh Kumar, AdeptJ
31 | */
32 | public final class ServerBootstrapper {
33 |
34 | /**
35 | * Bootstrap the given {@link Server} instance.
36 | *
37 | * @param server the {@link Server} (Tomcat, Jetty, Undertow) instance.
38 | * @param appConfig the application configuration.
39 | * @param args the program arguments to the server instance.
40 | */
41 | public static void bootstrap(Server server, Config appConfig, String[] args) throws Exception {
42 | Set> handleTypes = new LinkedHashSet<>();
43 | handleTypes.add(FrameworkLauncher.class);
44 | handleTypes.add(DefaultStartupAware.class);
45 | SciInfo sciInfo;
46 | // Undertow expects a ServletContainerInitializer class instance.
47 | if (server.getRuntime() == UNDERTOW) {
48 | sciInfo = new SciInfo(RuntimeInitializer.class, handleTypes);
49 | } else {
50 | sciInfo = new SciInfo(new RuntimeInitializer(), handleTypes);
51 | }
52 | ServletDeployment deployment = new ServletDeployment(sciInfo)
53 | .addServletInfo(new ServletInfo(ADMIN_SERVLET_NAME, ADMIN_SERVLET_URI, AdminServlet.class))
54 | .addServletInfo(new ServletInfo(ERROR_SERVLET_NAME, ERROR_SERVLET_URI, ErrorServlet.class))
55 | .addServletInfo(new ServletInfo(FAVICON_SERVLET_NAME, FAVICON_SERVLET_URI, FaviconServlet.class))
56 | .addServletInfo(new ServletInfo(LOGBACK_VIEW_SERVLET_NAME, LOGBACK_VIEW_SERVLET_URI,
57 | ViewStatusMessagesServlet.class));
58 | server.start(deployment, appConfig, args);
59 | }
60 |
61 | private ServerBootstrapper() {
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/core/StartupAware.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.core;
22 |
23 | import jakarta.servlet.ServletContext;
24 | import jakarta.servlet.ServletException;
25 |
26 | /**
27 | * This will be called by the ServletContainerInitializer while startup is in progress because this is
28 | * declared as a HandlesTypes.
29 | *
30 | * @author Rakesh.Kumar, AdeptJ
31 | */
32 | @FunctionalInterface
33 | public interface StartupAware {
34 |
35 | /**
36 | * This method will be called by the ServletContainerInitializer while startup is in progress.
37 | *
38 | * @param servletContext the {@link ServletContext} in which this StartupAware runs in.
39 | * @throws ServletException exception thrown by startup code
40 | */
41 | void onStartup(ServletContext servletContext) throws ServletException;
42 | }
43 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/exception/RuntimeInitializationException.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.exception;
22 |
23 | import java.io.Serial;
24 |
25 | /**
26 | * RuntimeInitializationException: Exception thrown by AdeptJ Runtime initialization code.
27 | *
28 | * @author Rakesh.Kumar, AdeptJ
29 | */
30 | public class RuntimeInitializationException extends RuntimeException {
31 |
32 | @Serial
33 | private static final long serialVersionUID = -1731078900410045078L;
34 |
35 | public RuntimeInitializationException(Throwable cause) {
36 | super(cause);
37 | }
38 | }
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/exception/ServerException.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.exception;
22 |
23 | import java.io.Serial;
24 |
25 | /**
26 | * ServerException: Exception thrown by AdeptJ Runtime.
27 | *
28 | * @author Rakesh.Kumar, AdeptJ
29 | */
30 | public class ServerException extends RuntimeException {
31 |
32 | @Serial
33 | private static final long serialVersionUID = -7931100309885768442L;
34 |
35 | public ServerException(Throwable cause) {
36 | super(cause);
37 | }
38 | }
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/html/I18nSupport.java:
--------------------------------------------------------------------------------
1 | package com.adeptj.runtime.html;
2 |
3 | import io.pebbletemplates.pebble.extension.AbstractExtension;
4 | import io.pebbletemplates.pebble.extension.Function;
5 | import io.pebbletemplates.pebble.extension.i18n.UTF8Control;
6 | import io.pebbletemplates.pebble.template.EvaluationContext;
7 | import io.pebbletemplates.pebble.template.PebbleTemplate;
8 | import org.apache.commons.lang3.StringUtils;
9 |
10 | import java.text.MessageFormat;
11 | import java.util.ArrayList;
12 | import java.util.HashMap;
13 | import java.util.List;
14 | import java.util.Map;
15 | import java.util.ResourceBundle;
16 |
17 | import static com.adeptj.runtime.common.Constants.VAR_ERROR_CODE;
18 |
19 | /**
20 | * This class replaces the default i18n extension provided by Pebble, this is done to cater AdeptJ Runtime requirements.
21 | *
22 | * @author Rakesh Kumar, AdeptJ
23 | */
24 | public class I18nSupport extends AbstractExtension implements Function {
25 |
26 | private final String rbDir;
27 |
28 | private final List argumentNames;
29 |
30 | public I18nSupport(String rbDir) {
31 | this.rbDir = rbDir;
32 | this.argumentNames = new ArrayList<>();
33 | this.argumentNames.add("bundle");
34 | this.argumentNames.add("key");
35 | this.argumentNames.add("params");
36 | }
37 |
38 | // ----------------------------------------- Extension Methods -----------------------------------------
39 |
40 | @Override
41 | public Map getFunctions() {
42 | Map functions = new HashMap<>();
43 | functions.put("i18n", this);
44 | return functions;
45 | }
46 |
47 | // ------------------------------------------ Function Methods ------------------------------------------
48 |
49 | @Override
50 | public List getArgumentNames() {
51 | return this.argumentNames;
52 | }
53 |
54 | @Override
55 | public Object execute(Map args, PebbleTemplate self, EvaluationContext context, int lineNumber) {
56 | String basename = (String) args.get("bundle");
57 | ResourceBundle rb = ResourceBundle.getBundle(this.rbDir + basename, context.getLocale(),
58 | new UTF8Control());
59 | String key = (String) args.get("key");
60 | Object errorCode = context.getVariable(VAR_ERROR_CODE);
61 | Object phraseObject;
62 | if (errorCode == null || StringUtils.equals(key, "go.home.msg")) {
63 | phraseObject = rb.getObject(key);
64 | } else {
65 | phraseObject = rb.getObject(errorCode + "." + key);
66 | }
67 | Object params = args.get("params");
68 | if (params == null) {
69 | return phraseObject;
70 | }
71 | if (params instanceof List> list) {
72 | phraseObject = MessageFormat.format(phraseObject.toString(), list.toArray());
73 | } else {
74 | phraseObject = MessageFormat.format(phraseObject.toString(), params);
75 | }
76 | return phraseObject;
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/html/TemplateProcessingException.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.html;
22 |
23 | import java.io.Serial;
24 |
25 | /**
26 | * The {@link TemplateProcessingException} thrown when an exception occurred while processing a given template.
27 | *
28 | * @author Rakesh.Kumar, AdeptJ.
29 | */
30 | final class TemplateProcessingException extends RuntimeException {
31 |
32 | @Serial
33 | private static final long serialVersionUID = -8077495079692215520L;
34 |
35 | TemplateProcessingException(Throwable cause) {
36 | super(cause);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/logging/DebugLevelHighlightingConverter.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.logging;
22 |
23 | import ch.qos.logback.classic.pattern.color.HighlightingCompositeConverter;
24 | import ch.qos.logback.classic.spi.ILoggingEvent;
25 |
26 | import static ch.qos.logback.classic.Level.DEBUG_INT;
27 | import static ch.qos.logback.core.pattern.color.ANSIConstants.YELLOW_FG;
28 |
29 | /**
30 | * Extended version of {@link HighlightingCompositeConverter} which prints debug log level in yellow.
31 | *
32 | * @author Rakesh.Kumar, AdeptJ
33 | */
34 | public class DebugLevelHighlightingConverter extends HighlightingCompositeConverter {
35 |
36 | @Override
37 | protected String getForegroundColorCode(ILoggingEvent event) {
38 | if (event.getLevel().toInt() == DEBUG_INT) {
39 | return YELLOW_FG;
40 | }
41 | return super.getForegroundColorCode(event);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/logging/LogbackInitializer.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.logging;
22 |
23 | import ch.qos.logback.classic.LoggerContext;
24 | import ch.qos.logback.classic.spi.Configurator;
25 | import ch.qos.logback.classic.spi.ConfiguratorRank;
26 | import ch.qos.logback.core.spi.ContextAwareBase;
27 | import ch.qos.logback.core.util.StatusPrinter2;
28 | import com.adeptj.runtime.common.LogbackManagerHolder;
29 | import com.adeptj.runtime.kernel.ConfigProvider;
30 | import com.typesafe.config.Config;
31 | import org.slf4j.bridge.SLF4JBridgeHandler;
32 |
33 | import static com.adeptj.runtime.common.Constants.LOGGING_CONF_SECTION;
34 |
35 | /**
36 | * This Class initializes the Logback logging framework.
37 | *
38 | * Usually Logback is initialized via logback.xml file on classpath but programmatic approach is faster and takes
39 | * about ~ 140 milliseconds to fully bootstrap Logback.
40 | *
41 | * Note: 05-Aug-2023 - {@link ConfiguratorRank} was introduced in Logback v1.4.9
42 | *
43 | * @author Rakesh.Kumar, AdeptJ
44 | */
45 | @ConfiguratorRank(value = ConfiguratorRank.CUSTOM_TOP_PRIORITY)
46 | public final class LogbackInitializer extends ContextAwareBase implements Configurator {
47 |
48 | /**
49 | * See class description for details.
50 | *
51 | * @param context the {@link LoggerContext}
52 | */
53 | @Override
54 | public ExecutionStatus configure(LoggerContext context) {
55 | Config loggingCfg = ConfigProvider.getInstance().getMainConfig().getConfig(LOGGING_CONF_SECTION);
56 | LogbackManager logbackManager = new LogbackManager(context);
57 | LogbackManagerHolder.getInstance().setLogbackManager(logbackManager);
58 | // Initialize ConsoleAppender.
59 | logbackManager.initConsoleAppender(loggingCfg);
60 | // Initialize RollingFileAppender.
61 | logbackManager.initRollingFileAppender(loggingCfg);
62 | // Update level and add appenders to ROOT Logger
63 | logbackManager.configureRootLogger(loggingCfg);
64 | // Add all the loggers defined in server.conf logging section.
65 | logbackManager.addServerConfigLoggers(loggingCfg);
66 | // SLF4J JUL Bridge.
67 | SLF4JBridgeHandler.removeHandlersForRootLogger();
68 | SLF4JBridgeHandler.install();
69 | // LevelChangePropagator - see http://logback.qos.ch/manual/configuration.html#LevelChangePropagator
70 | logbackManager.initLevelChangePropagator();
71 | // Finally, start LoggerContext and print status information.
72 | context.start();
73 | new StatusPrinter2().printInCaseOfErrorsOrWarnings(context);
74 | return ExecutionStatus.DO_NOT_INVOKE_NEXT_IF_ANY;
75 | }
76 | }
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/logging/OSGiLoggerConfig.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.logging;
22 |
23 | import java.util.Set;
24 |
25 | /**
26 | * Configurations for creating a Logback Logger.
27 | *
28 | * @author Rakesh.Kumar, AdeptJ
29 | */
30 | class OSGiLoggerConfig {
31 |
32 | private final String configPid;
33 |
34 | private final Set categories;
35 |
36 | private final String level;
37 |
38 | OSGiLoggerConfig(String configPid, Set categories, String level) {
39 | this.configPid = configPid;
40 | this.categories = categories;
41 | this.level = level;
42 | }
43 |
44 | public String getConfigPid() {
45 | return configPid;
46 | }
47 |
48 | public Set getCategories() {
49 | return this.categories;
50 | }
51 |
52 | public String getLevel() {
53 | return this.level;
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/osgi/BridgeHttpSessionIdListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.osgi;
22 |
23 | import jakarta.servlet.http.HttpSessionEvent;
24 | import jakarta.servlet.http.HttpSessionIdListener;
25 |
26 | /**
27 | * An {@link HttpSessionIdListener} which propagates the {@link jakarta.servlet.http.HttpSession} Id change
28 | * event to Felix {@link org.apache.felix.http.base.internal.EventDispatcher}.
29 | *
30 | * @author Rakesh.Kumar, AdeptJ
31 | */
32 | public class BridgeHttpSessionIdListener implements HttpSessionIdListener {
33 |
34 | @Override
35 | public void sessionIdChanged(HttpSessionEvent event, String oldSessionId) {
36 | HttpSessionEvents.handleSessionIdChangedEvent(event, oldSessionId);
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/osgi/BridgeHttpSessionListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016-2024, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.osgi;
22 |
23 | import jakarta.servlet.http.HttpSessionEvent;
24 | import jakarta.servlet.http.HttpSessionListener;
25 |
26 | /**
27 | * An {@link HttpSessionListener} which propagates the {@link jakarta.servlet.http.HttpSession} create and destroy
28 | * event to Felix {@link org.apache.felix.http.base.internal.EventDispatcher}.
29 | *
30 | * @author Rakesh.Kumar, AdeptJ
31 | */
32 | public class BridgeHttpSessionListener implements HttpSessionListener {
33 |
34 | @Override
35 | public void sessionCreated(HttpSessionEvent se) {
36 | HttpSessionEvents.handleHttpSessionEvent(HttpSessionEvents.SESSION_CREATED, se);
37 | }
38 |
39 | @Override
40 | public void sessionDestroyed(HttpSessionEvent se) {
41 | HttpSessionEvents.handleHttpSessionEvent(HttpSessionEvents.SESSION_DESTROYED, se);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/osgi/BridgeServletContextAttributeListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.osgi;
22 |
23 | import com.adeptj.runtime.common.BundleContextHolder;
24 | import org.apache.commons.lang3.StringUtils;
25 | import org.osgi.framework.BundleContext;
26 |
27 | import jakarta.servlet.ServletContextAttributeEvent;
28 | import jakarta.servlet.ServletContextAttributeListener;
29 |
30 | import static com.adeptj.runtime.common.Constants.ATTRIBUTE_BUNDLE_CONTEXT;
31 |
32 | /**
33 | * A {@link ServletContextAttributeListener} which initializes the {@link EventDispatcherTracker}
34 | * when {@link BundleContext} is being set as a {@link jakarta.servlet.ServletContext} attribute and again closes
35 | * and opens when {@link BundleContext} is replaced as a {@link jakarta.servlet.ServletContext} attribute.
36 | *
37 | * @author Rakesh.Kumar, AdeptJ
38 | */
39 | public class BridgeServletContextAttributeListener implements ServletContextAttributeListener {
40 |
41 | @Override
42 | public void attributeAdded(ServletContextAttributeEvent event) {
43 | if (StringUtils.equals(event.getName(), ATTRIBUTE_BUNDLE_CONTEXT)) {
44 | ServiceTrackers.getInstance().openEventDispatcherTracker((BundleContext) event.getValue());
45 | }
46 | }
47 |
48 | @Override
49 | public void attributeReplaced(ServletContextAttributeEvent event) {
50 | if (StringUtils.equals(event.getName(), ATTRIBUTE_BUNDLE_CONTEXT)) {
51 | ServiceTrackers.getInstance().closeEventDispatcherTracker();
52 | /*
53 | * Now open the EventDispatcherTracker with fresh BundleContext which is already hold by
54 | * BundleContextHolder after being set in FrameworkLifecycleListener.
55 | *
56 | * Rationale: If we use the BundleContext contained in the event passed which is a stale
57 | * BundleContext in case of a framework restart event and results in an IllegalStateException.
58 | */
59 | ServiceTrackers.getInstance()
60 | .openEventDispatcherTracker(BundleContextHolder.getInstance().getBundleContext());
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/osgi/DispatcherServletConfig.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 | package com.adeptj.runtime.osgi;
21 |
22 | import jakarta.servlet.ServletConfig;
23 | import jakarta.servlet.ServletContext;
24 | import java.util.Enumeration;
25 |
26 | /**
27 | * {@link ServletConfig} for Felix {@link org.apache.felix.http.base.internal.dispatch.DispatcherServlet}.
28 | *
29 | * @author Rakesh.Kumar, AdeptJ
30 | */
31 | public class DispatcherServletConfig implements ServletConfig {
32 |
33 | private final ServletConfig bridgeServletConfig;
34 |
35 | public DispatcherServletConfig(ServletConfig bridgeServletConfig) {
36 | this.bridgeServletConfig = bridgeServletConfig;
37 | }
38 |
39 | @Override
40 | public String getServletName() {
41 | return "Felix DispatcherServlet in Bridge Mode";
42 | }
43 |
44 | @Override
45 | public ServletContext getServletContext() {
46 | return this.bridgeServletConfig.getServletContext();
47 | }
48 |
49 | @Override
50 | public String getInitParameter(String name) {
51 | return this.bridgeServletConfig.getInitParameter(name);
52 | }
53 |
54 | @Override
55 | public Enumeration getInitParameterNames() {
56 | return this.bridgeServletConfig.getInitParameterNames();
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/osgi/DispatcherServletWrapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.osgi;
22 |
23 | import com.adeptj.runtime.kernel.util.Times;
24 | import org.slf4j.Logger;
25 | import org.slf4j.LoggerFactory;
26 |
27 | import jakarta.servlet.ServletConfig;
28 | import jakarta.servlet.ServletException;
29 | import jakarta.servlet.ServletRequest;
30 | import jakarta.servlet.ServletResponse;
31 | import jakarta.servlet.http.HttpServlet;
32 | import java.io.IOException;
33 | import java.io.Serial;
34 |
35 | /**
36 | * {@link HttpServlet} wrapper for Felix {@link org.apache.felix.http.base.internal.dispatch.DispatcherServlet}.
37 | *
38 | * @author Rakesh.Kumar, AdeptJ
39 | */
40 | public class DispatcherServletWrapper extends HttpServlet {
41 |
42 | @Serial
43 | private static final long serialVersionUID = 3090993776785941111L;
44 |
45 | private static final Logger LOGGER = LoggerFactory.getLogger(DispatcherServletWrapper.class);
46 |
47 | /**
48 | * The Felix {@link org.apache.felix.http.base.internal.dispatch.DispatcherServlet}
49 | */
50 | private final HttpServlet dispatcherServlet;
51 |
52 | DispatcherServletWrapper(HttpServlet dispatcherServlet) {
53 | this.dispatcherServlet = dispatcherServlet;
54 | }
55 |
56 | @Override
57 | public void init(ServletConfig config) throws ServletException {
58 | long startTime = System.nanoTime();
59 | super.init(config);
60 | this.dispatcherServlet.init(config);
61 | LOGGER.info("Felix DispatcherServlet initialized in [{}] ms!!", Times.elapsedMillis(startTime));
62 | }
63 |
64 | @Override
65 | public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
66 | this.dispatcherServlet.service(req, res);
67 | }
68 |
69 | @Override
70 | public void destroy() {
71 | this.dispatcherServlet.destroy();
72 | LOGGER.info("Felix DispatcherServlet Destroyed!!");
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/osgi/FrameworkEvents.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.osgi;
22 |
23 | import java.util.stream.Stream;
24 |
25 | /**
26 | * OSGi Framework event code to string mapping.
27 | *
28 | * @author Rakesh.Kumar, AdeptJ
29 | */
30 | public enum FrameworkEvents {
31 |
32 | STARTED(1),
33 |
34 | ERROR(2),
35 |
36 | PACKAGES_REFRESHED(4),
37 |
38 | STOPPED(64),
39 |
40 | STOPPED_UPDATE(128),
41 |
42 | WAIT_TIMED_OUT(512),
43 |
44 | UNKNOWN(-1);
45 |
46 | private final int code;
47 |
48 | FrameworkEvents(int code) {
49 | this.code = code;
50 | }
51 |
52 | public int getCode() {
53 | return code;
54 | }
55 |
56 | public static String asString(int code) {
57 | return Stream.of(values())
58 | .filter(fe -> code == fe.getCode())
59 | .findFirst()
60 | .orElse(UNKNOWN)
61 | .toString();
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/osgi/FrameworkLauncher.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.osgi;
22 |
23 | import com.adeptj.runtime.core.StartupAware;
24 |
25 | import jakarta.servlet.ServletContext;
26 |
27 | /**
28 | * FrameworkLauncher is a {@link StartupAware} that launches the OSGi Framework.
29 | *
30 | * @author Rakesh.Kumar, AdeptJ
31 | */
32 | public class FrameworkLauncher implements StartupAware {
33 |
34 | /**
35 | * This method will be called by the ServletContainerInitializer while startup is in progress.
36 | *
37 | * @param servletContext the {@link ServletContext}
38 | */
39 | @Override
40 | public void onStartup(ServletContext servletContext) {
41 | FrameworkManager.getInstance().startFramework();
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/osgi/FrameworkShutdownHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.osgi;
22 |
23 | import com.adeptj.runtime.common.ServletContextHolder;
24 | import com.adeptj.runtime.kernel.util.Times;
25 | import org.slf4j.Logger;
26 | import org.slf4j.LoggerFactory;
27 |
28 | import jakarta.servlet.ServletContextEvent;
29 | import jakarta.servlet.ServletContextListener;
30 | import jakarta.servlet.annotation.WebListener;
31 |
32 | /**
33 | * ContextListener that handles the OSGi Framework shutdown.
34 | *
35 | * @author Rakesh.Kumar, AdeptJ
36 | */
37 | @WebListener("Stops the OSGi Framework when ServletContext is destroyed")
38 | public class FrameworkShutdownHandler implements ServletContextListener {
39 |
40 | @Override
41 | public void contextDestroyed(ServletContextEvent event) {
42 | long startTime = System.nanoTime();
43 | Logger logger = LoggerFactory.getLogger(this.getClass());
44 | logger.info("Stopping OSGi Framework as ServletContext is being destroyed!!");
45 | ServiceTrackers.getInstance().closeEventDispatcherTracker();
46 | // see - https://github.com/AdeptJ/adeptj-runtime/issues/4
47 | // Close the DispatcherServletTracker here rather than in BridgeServlet#destroy method.
48 | // As with version 3.0.18 of Felix Http base the way with HttpSessionListener(s) handled
49 | // is changed which results in a NPE.
50 | ServiceTrackers.getInstance().closeDispatcherServletTracker();
51 | FrameworkManager.getInstance().stopFramework();
52 | ServletContextHolder.getInstance().setServletContext(null);
53 | logger.info("OSGi Framework stopped in [{}] ms!!", Times.elapsedMillis(startTime));
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/osgi/LoggerConfigFactoryListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.osgi;
22 |
23 | import com.adeptj.runtime.common.LogbackManagerHolder;
24 | import com.adeptj.runtime.logging.LogbackManager;
25 | import org.osgi.framework.ServiceEvent;
26 | import org.osgi.framework.ServiceListener;
27 | import org.slf4j.Logger;
28 | import org.slf4j.LoggerFactory;
29 |
30 | import java.util.concurrent.locks.Lock;
31 | import java.util.concurrent.locks.ReentrantLock;
32 |
33 | import static org.osgi.framework.ServiceEvent.REGISTERED;
34 | import static org.osgi.framework.ServiceEvent.UNREGISTERING;
35 |
36 | /**
37 | * OSGi {@link ServiceListener} for getting logger config properties from LoggerConfigFactory services.
38 | *
39 | * @author Rakesh.Kumar, AdeptJ
40 | */
41 | public class LoggerConfigFactoryListener implements ServiceListener {
42 |
43 | private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConfigFactoryListener.class);
44 |
45 | private final Lock lock;
46 |
47 | LoggerConfigFactoryListener() {
48 | this.lock = new ReentrantLock();
49 | }
50 |
51 | @Override
52 | public void serviceChanged(ServiceEvent event) {
53 | this.lock.lock();
54 | LogbackManager logbackManager = LogbackManagerHolder.getInstance().getLogbackManager();
55 | try {
56 | switch (event.getType()) {
57 | case REGISTERED -> logbackManager.addOSGiLoggers(event.getServiceReference());
58 | case UNREGISTERING -> logbackManager.resetLoggers(event.getServiceReference());
59 | default -> LOGGER.warn("Ignored ServiceEvent: [{}]", event.getType());
60 | }
61 | } finally {
62 | this.lock.unlock();
63 | }
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/main/src/main/java/com/adeptj/runtime/servlet/ErrorServlet.java:
--------------------------------------------------------------------------------
1 | /*
2 | ###############################################################################
3 | # #
4 | # Copyright 2016, AdeptJ (http://www.adeptj.com) #
5 | # #
6 | # Licensed under the Apache License, Version 2.0 (the "License"); #
7 | # you may not use this file except in compliance with the License. #
8 | # You may obtain a copy of the License at #
9 | # #
10 | # http://www.apache.org/licenses/LICENSE-2.0 #
11 | # #
12 | # Unless required by applicable law or agreed to in writing, software #
13 | # distributed under the License is distributed on an "AS IS" BASIS, #
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 | # See the License for the specific language governing permissions and #
16 | # limitations under the License. #
17 | # #
18 | ###############################################################################
19 | */
20 |
21 | package com.adeptj.runtime.servlet;
22 |
23 | import com.adeptj.runtime.html.TemplateEngine;
24 | import com.adeptj.runtime.html.TemplateEngineContext;
25 | import com.adeptj.runtime.kernel.util.Environment;
26 | import com.adeptj.runtime.kernel.util.RequestUtil;
27 | import jakarta.servlet.annotation.WebServlet;
28 | import jakarta.servlet.http.HttpServlet;
29 | import jakarta.servlet.http.HttpServletRequest;
30 | import jakarta.servlet.http.HttpServletResponse;
31 |
32 | import java.io.Serial;
33 |
34 | import static com.adeptj.runtime.common.Constants.ERROR_SERVLET_URI;
35 | import static com.adeptj.runtime.common.Constants.VAR_ERROR_CODE;
36 | import static jakarta.servlet.DispatcherType.ERROR;
37 |
38 | /**
39 | * ErrorServlet that serves the error page w.r.t error coded(401, 403, 404, 500).
40 | *