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 -      ...

[COMPONENT] What is the automatically generated div tag around a component used for / How to remove DIV tag is generated by CQ

Question: If a component is included for example by the <cq:include> tag, a div tag is added automatically around the component in the generated HTML. What is this div tag used for and is it possible to suppress the automatic creation of it? Resolution: Add the below code in to global.jsp, which you include for all the component jsp's. if (WCMMode.fromRequest(request) != WCMMode.EDIT && WCMMode.fromRequest(request) != WCMMode.DESIGN) {          IncludeOptions.getOptions(request, true).forceSameContext(Boolean.TRUE); } This will not generate any more extra <div> tags for any number of components you add. Reference: This div tag is used for the editing system of CQ5 (drawing the edit bars, rollover frames etc), the Designer system of CQ5 (set CSS class name, Designer CSS styling) and also for identifying the component in the client DOM. It's possible to suppress this generated div tag either...