How to deploy the broadleaf demo on my own Tomcat server? - mysql

I was able to start the Broadleaf demo application from the provided scripts adminstart.sh and startsite.sh. I was also able to change the database to MySQl. However, I do not understand how to create a war that can run on my Tomcat instance.
I see that broadleaf does things differently as compared to the common approach. Usually I put the context values into the file application-context. xml that is referred in web.xml. There is no need for modifying Tomcat context.xml. The example is
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-context.xml</param-value>
</context-param
In the broadleaf configuration there are several context parameters
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>blAdmin.root</param-value>
</context-param>
<context-param>
<param-name>patchConfigLocation</param-name>
<param-value>
classpath:/bl-open-admin-contentClient-applicationContext.xml
classpath:/bl-open-admin-contentCreator-applicationContext.xml
classpath:/bl-admin-applicationContext.xml
classpath:/bl-cms-contentClient-applicationContext.xml
classpath:/bl-cms-contentCreator-applicationContext.xml
classpath*:/blc-config/admin/bl-*-applicationContext.xml
classpath:/applicationContext.xml
classpath:/applicationContext-email.xml
/WEB-INF/applicationContext-datasource.xml
/WEB-INF/applicationContext-admin-security.xml
/WEB-INF/applicationContext-admin-filter.xml
/WEB-INF/applicationContext-admin.xml
</param-value>
</context-param>
<context-param>
<param-name>shutdownHookMethod</param-name>
<param-value>forceFlush</param-value>
</context-param>
Plus in addition in web.xml:
<!-- Initialize spring mvc -->
<servlet>
<servlet-name>admin</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/applicationContext-servlet-open-admin.xml
classpath:/applicationContext-servlet-admin.xml
classpath:/applicationContext-servlet-cms-contentClient.xml
classpath:/applicationContext-servlet-cms-contentCreator.xml
classpath:/bl-menu-admin-applicationContext-servlet.xml
classpath*:/blc-config/admin/bl-*-admin-applicationContext-servlet.xml
/WEB-INF/applicationContext-servlet-admin.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
The result is that the Tomcat instance contains context.xml in the conf directory that provides database pool configuration:
What is the best way of utilizing broadleaf in my application?

Configuration
Put your MySQL JDBC driver to tomcat7/lib folder
Open web.xml for both, site and admin modules in DemoSite
Add the following code in both web.xml
<resource-ref>
<description>WEB</description>
<res-ref-name>jdbc/web</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<description>STORAGE</description>
<res-ref-name>jdbc/storage</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<description>SECURE</description>
<res-ref-name>jdbc/secure</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Open cmd prompt for windows or command shell for linux
Navigate to your JDK_directory/bin and create keystore for SSL switching
keytool -genkey -alias tomcat -keyalg RSA -storepass password
Open Tomcat7/conf/server.xml
Add the following code to enable SSL switching, just below your default connector port
<Connector
protocol="org.apache.coyote.http11.Http11NioProtocol"
port="8443" maxThreads="200"
scheme="https" secure="true"
SSLEnabled="true"
keystorePass="password"
clientAuth="false"
sslProtocol="TLS"
/>
Open Tomcat7/conf/tomcat-users.xml
Add following
<user username="tomcat" password="password" roles="manager-gui"/>
Populate database.properties with values for your database or
Configure JNDI for tomcat
In your admin or site folder there is file target/tomcat-server-config/context.xml.
Copy this file into tomcat7/conf/
Open this file and replace the following properties with your database values
username="${database.user}" : Replace with your database username (eg. broadleaf)
password="${database.password}" : Replace with your database password
driverClassName="${database.driver}" : Replace with your database classname (eg. org.postgresql.Driver)
url="${database.url}" : Replace with your database URL with database path, port and name. (eg. jdbc:postgresql://localhost:5432/broadleaf)
in your IDE Start Maven project install goal of lifecycle (of entire project or each module)
This will create wars for each module
Start tomcat (run startup.bat/startup.sh in ~/tomcat7/bin/ )
Go to app manager and enter tomcat username and key that you added into tomcat-users.xml
deploy admin and site wars
context path is url mapping (eg. /admin and /demo-site)
XML configuration url is full path to your targets context.xml (eg. ~/target/tomcat-server-conf/context.xml)
WAR directory is full path to your war in target directory
Don't forget to change blPU.hibernate.hbm2ddl.auto in development.properties from create to update (standalone tomcat have trouble to create tables - that's my expirience with postreSQL)

Related

JavaMelody JDBC Graph not working Tomcat 8.8 and mysql DB

I have configured javamelody 1.86.0 on Tomcat 8.8 and all the other metrics are captured except from the JDBC. Below are the config I have on the Tomcat application:
Context.xml:
<ResourceLink type="javax.sql.DataSource"
name="jdbc/LocalAPIDB"
global="jdbc/APIDB"/>
Server.xml:
<Resource type="javax.sql.DataSource"
name="jdbc/APIDB"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/apiDB"
username="xxxx"
password="xxxxx"
initialSize="340"
maxActive="3770"
maxIdle="2330"
minIdle="890"
testOnBorrow="true"
validationQuery="SELECT 1"
validationInterval="30000"
timeBetweenEvictionRunsMillis="5000"
minEvictableIdleTimeMillis="60000"
removeAbandoned="true"
removeAbandonedTimeout="60"
abandonWhenPercentageFull="0"
logAbandoned="true"
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;
org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;org.apache.tomcat.jdbc.pool.interceptor.ResetAbandonedTimer"/>
On mbeans I can see there are active connections but on the graph nothing is captured.
JDBC Graph
MBeans
Debugging Logs
I suggest that you move the Resource from server.xml to the xml context file of the webapp (conf/Catalina/localhost/MpesaIntegrations.xml) and that you remove the ResourceLink.
So the xml context file of the webapp is like this:
<?xml version="1.0" encoding="UTF-8" ?>
<Context>
<Resource type="javax.sql.DataSource"
name="jdbc/LocalAPIDB"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/apiDB"
username="xxxx"
password="xxxxx"
initialSize="340"
maxActive="3770"
maxIdle="2330"
minIdle="890"
testOnBorrow="true"
validationQuery="SELECT 1"
validationInterval="30000"
timeBetweenEvictionRunsMillis="5000"
minEvictableIdleTimeMillis="60000"
removeAbandoned="true"
removeAbandonedTimeout="60"
abandonWhenPercentageFull="0"
logAbandoned="true"
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;org.apache.tomcat.jdbc.pool.interceptor.ResetAbandonedTimer"
/>
</Context>
And/or, if that's not enough, you probably have a listener or filter in your webapp that is getting and keeping in cache the DataSource. In that case, put the javamelody listener and filter near the top of your WEB-INF/web.xml file.
For anybody scratching their head trying to find out why JavaMelody won't show database stats such as 'active JDBC connections', I finally solved it by doing 2 things. Note that in my case I'm using Java 11 running Apache Tomcat 9 hosting an Apache Derby 10.15.2 database:
Setting up the database in the 'apache-tomcat-9.0.52\conf\context.xml' file eather than in java code in my webapp. I think this made the database associated with the JNDI resource name so JavaMelody could see it. I added this XML to that context.xml file:
<Resource name="jdbc/DatabaseJNDIResourceName"
auth="Container"
type="javax.sql.DataSource"
driverClassName="org.apache.derby.jdbc.EmbeddedDriver"
url="jdbc:derby:../DatabaseName;"
username="someuser"
password="s3cret"
/>
Then you can get the DataSource in your webapp java code using:
InitialContext initialContextJNDI = new InitialContext();
DataSource dataSource = (DataSource)initialContextJNDI.lookup("java:comp/env/jdbc/DatabaseJNDIResourceName");
Note that a method to detect a potential JNDI resource name problem is to see if the resource name shows up in JavaMelody by navigating to the page 'JNDI tree' at the bottom, linked here http://localhost:8080/monitoring?part=jndi
Inserting the web.xml code referred to here. Interestingly, when that XML code was left out, javamelody would still run but it didn't report JDBC-related data.
<filter>
<filter-name>javamelody</filter-name>
<filter-class>net.bull.javamelody.MonitoringFilter</filter-class>
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>javamelody</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ASYNC</dispatcher>
</filter-mapping>
<listener>
<listener-class>net.bull.javamelody.SessionListener</listener-class>
</listener>
Another thing, even after these fixes finally solved the problem and made JavaMelody aware of my database, I didn't immediately notice since the graphs barely change if your database queries are fast. I only confirmed that the database stats were being logged after executing a long SQL query and updating the 'opened jdbc connections' page, linked at the bottom of the javamelody stats page here, which showed one single connection while the database took a few seconds to process the large (1 million row table) query http://localhost:8080/monitoring?part=connections

Use environment variables inside google-repo manifest?

We are trying to start using google-repo in our project as the project is divided into multiple repositories. The problem is that our git server requires that one puts the username into the URL, e.g.
git clone ssh://username#git.server.com
But is it possible to get that into the manifest? I've tried the following
<?xml version="1.0" encoding="UTF-8"?>
<manifest>
<remote name="gerrit"
fetch="ssh://$USER#git.server.com"
review="ssh://$USER#git.server.com"
revision="refs/heads/master"/>
<default remote="gerrit" sync-j="4"/>
<project name="project" remote="gerrit" path="project"/>
</manifest>
but google-repo simply uses ssh://$USER#git.server.com when cloning (that is, it does not dereference environment variable $USER).
This is a ssh config issue, you should not add $USER in the remote of your manifest.
In ~/.ssh/config, add:
host whatever git.server.com
IdentityFile ~/.ssh/id_rsa
User <your_user>
IdentityFile should link to your ssh private key (YMMV).
You should now be able to
git clone ssh://git.server.com

How to set aspnetcore_environment in publish file?

I have ASP.NET Core application (Web Api). The documentation has explained working with multiple environments, however it failed to explain how to set aspnetcore_environment when publishing the web site.
So lets say if i have 3 environments Development, Staging and Production
In classic ASP.NET Web Application i used to create 3 build configurations. Development, Staging and Production ( Like shown in picture below). and then 3 .pubxml files, one for each configuration. Do i need to use the same approach for ASP.NET Core application as well?
How do i set aspnetcore_environment in .pubxml file?
If the approach specified in Question 1 is obsolete, then what's the alternate approach? ( I use Jenkins for CI)
Update 1
I understand that I have to set ASPNETCORE_ENVIRONMENT however I am not able to understand where do we set this? During development I can set this in profile in launchSettings.json, however question was how do we set this when publishing to staging or production? do we set environment variable on the target server itself?
Update 2
I found article here that explains different ways of setting environment variable. This partially answered my question. However when I publish the application, the publish process does not honor the environment variable while publishing appsettings.{env.EnvironmentName}.json
I have created separate post for that question
You could pass in the desired ASPNETCORE_ENVIRONMENT into the dotnet publish command as an argument using:
/p:EnvironmentName=Staging
e.g.
dotnet publish /p:Configuration=Release /p:EnvironmentName=Staging
This will generate out the web.config with the correct environment specified for your project:
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Staging" />
</environmentVariables>
I had the same requirement, and I came up with the following solutions. This works well with automated deployments and require fewer configuration changes.
1. Modifying the project file (.CsProj) file
MSBuild supports the EnvironmentName Property which can help to set the right environment variable as per the Environment you wish to Deploy. The environment name would be added in the web.config during the Publish phase.
Simply open the project file (*.csProj) and add the following XML.
<!-- Custom Property Group added to add the Environment name during publish
The EnvironmentName property is used during the publish for the Environment variable in web.config
-->
<PropertyGroup Condition=" '$(Configuration)' == '' Or '$(Configuration)' == 'Debug'">
<EnvironmentName>Development</EnvironmentName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' != '' AND '$(Configuration)' != 'Debug' ">
<EnvironmentName>'$(Configuration)'</EnvironmentName>
</PropertyGroup>
Above code would add the environment name as Development for empty or Debug configuration. For any other Configuration the Environment name would be picked from the configuration which was selected. This will add the ASPNETCORE_ENVIRONMENT environment with the desired configuration. You can modify the logic for environment name as desired by updating the CsProj file. More details here
2. Adding the EnvironmentName Property in the publish profiles.
We can add the <EnvironmentName> property in the publish profile as well. Open the publish profile file which is located at the Properties/PublishProfiles/{profilename.pubxml} This will set the Environment name in web.config when the project is published. More Details here
<PropertyGroup>
<EnvironmentName>Development</EnvironmentName>
</PropertyGroup>
As shown in above image, environment can be added for each configuration and the name of the EnvironmentName property can be changed in each *.pubxml file.
3. Command line options using dotnet publish
Additionaly, we can pass the property EnvironmentName as a command line option to the dotnet publish command. Following command would include the environment variable as Development in the web.config file.
dotnet publish -c Debug -r win-x64 /p:EnvironmentName=Development
When hosting the application under IIS you can set the environment variable in web.config.
https://learn.microsoft.com/en-us/aspnet/core/hosting/aspnet-core-module
To generate it on publish add a web.config to the root of your project, "dotnet publish" will use this file as the basis for the one that is generated for in the publish folder. Then you can change the value in your deployment system.
<?xml version="1.0" encoding="utf-8" ?>
<!-- Used to overwrite settings web.config generated by "dotnet publish", Only used when hosting under IIS -->
<configuration>
<system.webServer>
<aspNetCore stdoutLogEnabled="true">
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
</aspNetCore>
</system.webServer>
</configuration>
I think you can't do it in the publish profile. You have to set environment variable, e.g. ASPNETCORE_ENVIRONMENT = Staging.
I had to do a similar thing with a aspnet core web app on Azure. I wanted to have dev, staging and production. The way I did it was exactly with env variable.
To setup two or more profiles, you need to create additional profile, as mentioned in a linked article, and your launchSettings.json will contain an array:
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express (Staging)": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Staging"
}
}
}
To be able to read the environment variable, you need to specify it during startup and call additional method AddEnvironmentVariables to variables take action:
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
// general properties
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
// specify the environment-based properties
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
// do not forget to add environment variables to your config!
.AddEnvironmentVariables();
Configuration = builder.Build();
}
}
Simple way to set it in visual studio IDE.
Project > Properties> Debug > Environment variables
Please do not use environment variables of machine level instead scope
to the application , there is a possibility of other application doing
same, changing may affect other application.

Installing WSO2 greg and API manager on different VM with shared MySQL

I'm installing greg and APi manager using this configuration:
1 VM for MySQL
1 VM for API
1 VM for BAM
1 VM for greg
All latest currrent releases
I'm wandering which is the best configuration.
My idea was having all the tool to place data in MySQL, but I'm wandering on how to ling all the DB in the various config files
APi should point to registry runniong on his VM and to the BAM instance, but looking on the registru moaster-datasource.xml I found also this:
<datasource>
<name>WSO2AM_DB</name>
<description>The datasource used for API Manager database</description>
<jndiConfig>
<name>jdbc/WSO2AM_DB</name>
</jndiConfig>
<definition type="RDBMS">
<configuration>
<url>jdbc:h2:repository/database/WSO2AM_DB;DB_CLOSE_ON_EXIT=FALSE</url>
<username>wso2carbon</username>
<password>wso2carbon</password>
<driverClassName>org.h2.Driver</driverClassName>
<maxActive>50</maxActive>
<maxWait>60000</maxWait>
<testOnBorrow>true</testOnBorrow>
<validationQuery>SELECT 1</validationQuery>
<validationInterval>30000</validationInterval>
</configuration>
</definition>
</datasource>
That imply that registry has access to API manager DB ... what for?
I'll have to add also ESB and BPS so this deployment architecture becomes complex.
Any hints
Thanks
Luca
WSO2 servers are using H2 database which is shipped by default. So if you start up a fresh pack which will run on its own database instance.
In order to share the registry data between servers, you will have to configure registry mounting. This will be helpful to create JDBC mounting. But if you have one instance of a server, then you can omit clustering part from the article. You can follow the same configuration for other servers as well (BAM, API manager and BPS) to configure JDBC mounting. So that all your data will be stored in MySql and shared across servers.
If you need to store users information also in JDBC and share it across servers, then you will have to add another data source into master-datasource.xml and use it in CARBON-HOME/repository/conf/user-mgt.xml instead of the default data source.
API manager is having few different data sources for its own purposes. That doesn't mean to have any relationship with registry. By default "WSO2CarbonDB" is only used for registry and user manager purposes as stated in the description of each data sources.
You can use mounted database to resolve your issue . please refer [1] for how to mount guide.
[1]. http://ajithvblogs.blogspot.com/2012/09/create-jdbc-mount-to-wso2-governance.html
Thank You,
Dharshana

How to set up a password for openrdf workbench?

I have successfully installed Openrdf Repository (sesame 2.3.2) and Openrdf workbench however I do not know how to set up a user and a password to protect Openrdf workbench. I suppose that there is --somewhere -- a configuration file.
Can somebody give me a hint how to create a user and set up a password for openrdf workbench?
I believe you need to password protect at the servlet container level. Are you using Tomcat? Here's what I used to set up basic authentication with Tomcat 6:
web.xml
<security-constraint>
<web-resource-collection>
<web-resource-name>Sesame Workbench</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>sesame-user</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Sesame Workbench</realm-name>
</login-config>
<security-role>
<description>The role required for Sesame workbench</description>
<role-name>sesame-user</role-name>
</security-role>
tomcat-users.xml
<role rolename="sesame-user"/>
<user username="workbench" password="workbench" roles="sesame-user"/>