Chuyển đến nội dung chính

[DATABASE] USE DATASOURCEPOOL WITHIN OSGI BUNDLE TO CONNECT MYSQL

Goal:

Create a CQ/AEM project to connect mysql by


Create project:



mvn archetype:generate \
   -DarchetypeRepository=http://repo.adobe.com/nexus/content/groups/public/ \
   -DarchetypeGroupId=com.day.jcr.vault \
   -DarchetypeArtifactId=multimodule-content-package-archetype \
   -DarchetypeVersion=1.0.2 \
   -DgroupId=mysqldemo \
   -DartifactId=mysqldemo \
   -Dversion=1.0-SNAPSHOT \
   -Dpackage=com.mysqldemo \
   -DappsFolderName=mysqldemo \
   -DartifactName="Mysql Demo" \
   -DcqVersion="5.6.1" \
   -DpackageGroup="Mysql Demo"


Update bundle pom:



<dependency>
           <groupId>com.day.commons</groupId>
           <artifactId>day.commons.datasource.poolservice</artifactId>
           <version>1.0.10</version>
           <scope>provided</scope>
</dependency>


Update content pom:


<dependencies>
...
<dependency>
           <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.22</version> <scope>provided</scope>
</dependency>
...
</dependencies>

...

<embeddeds>
...
<embedded> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <target>${install.target}</target> </embedded>
...
</embeddeds>



Configure the DataSourcePool connection properties:
Add a configuration for the JDBC Connections Pool service that uses the JDBC driver to create data source objects. The OSGi bundle created in this development article uses this service to connect to the MySQL database. For information, see Connecting to SQL Databases.  
To configure a DataSourcePool, peform these tasks:
1. Login to Adobe CQ’s Apache Felix Web Console at http://server:port/system/console/bundles (default admin user = admin with password= admin).
2. Click the Configuration tab..
3. Click the + icon that appearts in the JDBC Configuration Pool row (in AEM 6, the row is titled Day Commons JDBC Connections Pool).
4. Enter the following values:
  • JBDC Driver class - the driver class to use to connect to the database. To connect to MySQL, enter com.mysql.jdbc.Driver.
  • JDBC connection URI - the URI to the database. In this example, enter jdbc:mysql://localhost:3306/databasename.
  • Username - the user name to use to connect to MySQL.
  • Password - the corresponding password.
  • Datasource name - the datasource name. This is the value that you reference in the Java logic located in the OSGi bundle. In this example, enter Customer.
5. Click Save.


Code to get connection:



           @Reference
private DataSourcePool source;

// Returns a connection using the configured DataSourcePool
private Connection getConnection() {
DataSource dataSource = null;
Connection con = null;
try {
// Inject the DataSourcePool right here!
dataSource = (DataSource) source.getDataSource("Customer");
con = dataSource.getConnection();
return con;

} catch (Exception e) {
e.printStackTrace();
}
return null;
}

Nhận xét

Bài đăng phổ biến từ blog này

How to custom CQ Login Module

In order to manage the login process in our project, we will use a custom CQ Login Module. We will admit the root URL of CQ instance is: http://localhost:4502/ . This value may change depending of your environment. The %CQ_HOME% variable we will mention refers to the CQ install path. It admits you have defined %CQ_HOME% as an environment variable. 1.         Update the repository definitions The login module must be referenced in the repository definitions. You have to edit the next file: %CQ_HOME%/crx-quickstart/repository/repository.xml Do a copy of repository.xml to repository.xml.original In repostiory.xml, replace  security  part of repository.xml with following: <Security appName="com.day.crx">         <SecurityManager class="com.day.crx.core.CRXSecurityManager"> <WorkspaceAccessManager class="org.apache.jackrabbit.core.security.simple.SimpleWorkspaceAccessManager"/>  ...

[DAM] Custom DAM management / Add 'Alt' into images

Issue: We need, for each asset to be able to edit the ‘alt’ text. The ‘alt’ text must be used on the site each time an asset is displayed. When displaying an asset, the mechanism is the following : -       Get the Locale from the request -       When getting the asset, get the associated Alt property corresponding to the locale. -       Populate the alt attribute with this value. Resolution: To achieve this, we can customize the DAM Asset Editor. The AssetEditor is the form used to input asset properties and metadata. Below is a screenshot of a customized form with four “alt” input (for four different locales). The DAM uses the following nodes to render Asset Editor forms: -       /libs/dam/content/asseteditors/formitems -       /libs/dam/content/asseteditors/images/formitems -      ...

[CONFIGURATION] Change default start page

Change the default startup page from projects.html to welcome on login in Adobe CQ 5.6 and above. CQ5.6 introduced responsive layout and the landing page after logging into the author instance goes to the new touch friendly interface. However, as a developer I prefer the old interface where I can quicky navigate to any of the options. Author instance: So to change the landing page post login in the Author instance to go to the welcome page here’s what you need to do Go to CRX Explorer Interface http://localhost:4502/crx/explorer/browser/index.jsp Navigate to the node /libs/cq/core/config. author /com.day.cq.commons.servlets.RootMappingServlet Right click the rootmapping.target property and select edit. Change the value to /welcome.html Click Save All Publish instance: Go to CRX Explorer Interface http://localhost:4502/crx/explorer/browser/index.jsp Navigate to the node /libs/cq/core/config. publish /com.day.cq.commons.servlets.RootMappingServlet R...