Ambiguous dependency: Help me!!

42 views
Skip to first unread message

Riccardo Esposito

unread,
Sep 11, 2014, 11:02:44 AM9/11/14
to cdiadv...@googlegroups.com
Folks,

 I need your help!!

I'm approaching to CDI and I do not find the solution to this problem.

Environment:
IDE: Jboss Developer Studio version 7.1.1.GA
AS: jboss-as-7.1.1.Final


Description:
I have to import some xml files, those files have a different xlst stylesheet.
The idea is to to write a console. So that I can choose the "strategy", the file to import and other settings.
Then run the import process. 

So the problem I have with the cdi is (Just remember: "There can be only one.") to instantiate at runtime the right class.
At compiling time I get the cdi ambiguous dependencies message.
I'm also not familiar with annotations.

Could you help me?!

I wrote an abstract class IImportProcess containing the method to write the status of the process on the db.

public abstract class IImportProcess {

private static final Integer START = new Integer(1);
private static final Integer END = new Integer(0);
private static final Integer ERROR = new Integer(2);
@Inject
protected transient Logger logger;
@Inject private FileDAO dao;

private ArrayList<XDocsResource> resources_ = new ArrayList<XDocsResource>();
private String processName_;
private String stepName_;
private Date calDate_;
public void runImport() {
}
public void changeStatusStart() {
logger.debug("changeStatusStart: processName " + processName_ + " stepName " + stepName_);
int res = dao.changeStepStatus(processName_, stepName_, START);
logger.debug("changeStatusStart: number of record updated " + res);
}

public void changeStatusEnd() {
logger.debug("changeStatusEnd: processName " + processName_ + " stepName " + stepName_);
int res = dao.changeStepStatus(processName_, stepName_, END);
logger.debug("changeStatusEnd: number of record updated " + res);
}
public void changeStatusError() {
logger.debug("changeStatusError: processName " + processName_ + " stepName " + stepName_);
int res =dao.changeStepStatus(processName_, stepName_, ERROR);
logger.debug("changeStatusError: number of record updated " + res);
}

public String getMem() {
String res = "";
float tot, lib, used;
Runtime rt = Runtime.getRuntime();

tot = (float) rt.totalMemory();
tot = (tot / 1024) / 1024;
lib = ((float) rt.freeMemory() / 1024) / 1024;
used = tot - lib;
res = "Memory Occupation: Total " + tot + " Mb - Free " + lib
+ " Mb - Used " + used + " Mb";
return res;
}

public ArrayList<XDocsResource> getResources() {
return resources_;
}
public void setResources(ArrayList<XDocsResource> resource) {
resources_ = resource;
}

public String getProcessName() {
return processName_;
}
public void setProcessName(String processName) {
processName_ = processName;
}

public String getStepName() {
return stepName_;
}
public void setStepName(String stepName) {
stepName_ = stepName;
}

public Date getCalDate() {
return calDate_;
}
public void setCalDate(Date calDate) {
calDate_ = calDate;
}

}

Then I have two different classes that extends the IImportProcess (for space reason I show you just one).

@Inventory
public class ImportXmlInventory extends IImportProcess {

@Override
public void runImport() {
long  start, stop;
float exTime;
   
int recNum = 0;
 
 
logger.info("Capacity Control System:  test Monograms XML importer: ");
logger.info(getMem());
System.out.println("Capacity Control System:  test XML importer");
start = System.currentTimeMillis();
XTransformer tf = new XTransformer();
TxDataParser prs = new TxDataParser();
boolean testRes=false;
 /*elaborazione files*/
 for(XDocsResource srcDoc: getResources()){
 System.out.println("resource data doc: " + srcDoc.getSourceDocPath());
 System.out.println("resource data xsl: " + srcDoc.getSourceXslPath());
 System.out.println("resource data dest: " + srcDoc.getDestDocPath());
 tf.setSourceXmlDoc(srcDoc.getSourceDocPath());
 tf.setXslDocument(srcDoc.getSourceXslPath());
 tf.setDestinationXmlDoc(srcDoc.getDestDocPath());
 System.out.println("Start xml transformation:  " +srcDoc.getSourceDocPath());
 logger.debug("Start xml transformation:  " +srcDoc.getSourceDocPath());
 try{
 testRes = tf.transformDoc();
 }
 catch (Exception e) {
 logger.error(e);
 e.printStackTrace();
 }
 logger.debug("Xml transformed:  " + srcDoc.resourceExists(XDocType.XmlDest));
 if(srcDoc.resourceExists(XDocType.XmlDest)){
 logger.debug("Import transformed data :  " + srcDoc.getDestDocPath());
 System.out.println("Import transformed data :  " + srcDoc.getDestDocPath());
 prs = new TxDataParser();
 prs.parseDocument(srcDoc.getDestDocPath());
 List<DBRecord> recs = prs.getRecordList();
 System.out.println("Parsed data: SQL - " + prs.getSQLString());
 logger.debug("Parsed data: SQL - " + prs.getSQLString());
 System.out.println("Records added: " + recs.size());
 logger.debug("Records added: " + recs.size());
 recNum +=recs.size();
 for(DBRecord el: recs){
 logger.debug("start record ");
 List<DBColumn> cols = el.getColumns();
 for(DBColumn cEl: cols){
 logger.debug("Column :  " + cEl.toString());
 }
 logger.debug("end record ");
 }
 }
 }
 stop = System.currentTimeMillis();
 exTime = ((float)(stop - start)) /1000;
 logger.info(getMem());
 logger.info("Capacity Control System: XML importer - Total execution time: (sec) "+exTime + " - Total records elaborated: " + recNum);
 System.out.println("Capacity Control System: XML importer - Total execution time: (sec) "+exTime + " - Total records elaborated: " + recNum);
 
}
}


Then I have a factory, how  should get the right qualifier?

public class ImportFactory {

@Inject @Any Instance<IImportProcess> importProcess_;
IImportProcess importProcessInstance_;
@Produces
public IImportProcess getImportProcess(String type) {
Annotation qualifier = (type.compareTo("XML_TRANSMISSION") == 0 ? new TransmissionQualifier() : new InventoryQualifier());
importProcessInstance_ = (IImportProcess) importProcess_.select(qualifier);
return importProcessInstance_;
}
}


and the qualifiers:

@Qualifier
@Target({TYPE, METHOD, FIELD, PARAMETER})
@Retention(RUNTIME)
public @interface Transmission {}



public abstract class TransmissionQualifier extends AnnotationLiteral<Transmission> implements Transmission {}


@Qualifier
@Target({TYPE, METHOD, FIELD, PARAMETER})
@Retention(RUNTIME)
public @interface Inventory {}



public abstract class InventoryQualifier extends AnnotationLiteral<Inventory> implements Inventory {}

Where is the trouble?

Thank you in advance,
RE

Reply all
Reply to author
Forward
0 new messages