⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sampleprintserviceagent.java

📁 CmisJavaApi
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	// getting the stack (association from which the indication has been received	CMISAgentStack st=evt.getStack();	// getting the indication	SetIndication ind=evt.getIndication();	 // informing the external user	 System.out.println("A Set indication has been received...");	 System.out.println(ind.toString());	 // getting the concerned MO instance	 try	     {		 PrintJob pj = null;		 boolean error = false;		 try {		      pj = (PrintJob) mib.getManagedObject(ind.getBaseObjectInstance().getDistinguishedName());		 }		 catch (NoSuchObjectInstanceException e)		     {			 error = true;			 // A CMIS Error should be issued			 System.out.println("No such Object foudn in the agent");			 // lets also generate an Event-Report			 EventReportRequest er = st.newEventReportRequest();			 er.setMode(Mode.UNCONFIRMED);			 er.setEventType(new EventTypeId("1.152.4.1.1.1"));			 er.setManagedObjectClass(ind.getBaseObjectClass());			 er.setManagedObjectInstance(ind.getBaseObjectInstance());			 er.setEventInformation(new RMIAny (new OctetString("A generated Event Report")));			 try			     {				 er.doIt();			     }			 catch (CMISException erEx) {			     System.err.println("GenerateEventReportThread failed: " + erEx);			 }		     }		 if (error == false)		     {			 // collecting the attributes to be changed			 ModificationAttributeList list = ind.getModificationList();			 if (list == null)			     {				 			     }			 else			     {     				 // Enumerates all attributes				 				 System.out.println("Before iteration");				 for (Enumeration e=list.getElts(); e.hasMoreElements();)				     {					 // Algo si papertypes est le seul attribut present OK					 // sinon on balance un CMIS Error					 ModificationAttribute attrib = (ModificationAttribute) e.nextElement();					 System.out.println("Testing the attribute");					 if (attrib.getAttributeId().toString().equals("1.512.4.1.3") == true && error == false)					     {						 System.out.println("c'est le bon attribut");						 pj.setPaperType((OctetString) attrib.getValue());						 // Lets build the response						 SetResponse resp = st.newSetResponse();						 resp.setInvokeId(ind.getInvokeId());						 resp.setManagedObjectClass(ind.getBaseObjectClass());						 resp.setManagedObjectInstance(ind.getBaseObjectInstance());						 AttributeList al = new AttributeList();						 al.add(attrib.getAttributeId(),(OctetString) attrib.getValue());						 resp.setAttributeList(al) ;						 try						     {							 resp.doIt();						     } catch (CMISException cmisException)							 {}					     }					 else					     {						 error = true;					     }					 				     }			     }		     }		 else		     {			 SetResponse resp = st.newSetResponse();			 resp.setInvokeId(ind.getInvokeId());			 resp.setManagedObjectClass(ind.getBaseObjectClass());			 resp.setManagedObjectInstance(ind.getBaseObjectInstance());			 try			     {				 resp.doIt();			     } catch (CMISException cmisException)				 {}		     }	     }	 catch (Exception e)	     {System.out.println("An exception occured !");}    }}class SamplePrintServiceDeleteListener implements DeleteListener {    public ManagedObjectTree mib;    public PrintQueue printQueue;    public SamplePrintServiceDeleteListener(ManagedObjectTree pMib, PrintQueue pPq)    {	super();	mib = pMib;	printQueue = pPq;    }    public void perform( DeleteEvent evt )    {	// getting the stack (association from which the indication has been received	CMISAgentStack st=evt.getStack();	// getting the indication	DeleteIndication ind=evt.getIndication();	 // informing the external user	 System.out.println("A Delete indication has been received...");	 // checking that the right scope and objects are selected for deletion	 if (ind.getScope().getScope() == Scope.FIRSTLEVELONLY.getScope() && ind.getBaseObjectInstance().toString().equals(printQueue.getManagedObjectInstance().toString()) == true )	     {		 System.out.println("Delete is ok and can be performed");	// collecting usefull parameters	 ObjectClass baseObjectClass  = ind.getBaseObjectClass();	 ObjectInstance baseObjectInstance  = ind.getBaseObjectInstance();	 // telling if more than one object has been selected	 boolean many = false;	 // bulding a new invoke Id if several objects are to be returned	 int id = ind.getInvokeId().getInvokeId()*2;	 Vector v = new Vector();	 // selecting affected objects and building responses	 try	     {		 // getting all selected objects		 Enumeration en = mib.getManagedObjects(baseObjectInstance,ind.getScope(),null);		 // for each managed object, we build a CMIS Get Response		 for (Enumeration enum = en; enum.hasMoreElements() ;)		     {			 System.out.println("Debut de l'iteration");			 ManagedObjectInterface mo = (ManagedObjectInterface) enum.nextElement();			 System.out.println("Debut de l'iteration 2");			 DistinguishedName dn = mo.getManagedObjectInstance().getDistinguishedName();			 System.out.println("On commence le delete");			 // On ne peut pas faire le remove ici			 // on se prend une exception de concurrent access			 v.add(dn);			 //mib.remove(dn);			 DeleteResponse rsp= st.newDeleteResponse();			 rsp.setManagedObjectClass( mo.getManagedObjectClass());			 rsp.setManagedObjectInstance( mo.getManagedObjectInstance());			 // Assingning the invoke and linked ID			 if (enum.hasMoreElements() == true && many == false)			     {many = true;}			 if (many == true)			     {				 // there are several responses				 if (enum.hasMoreElements() == true)				     {					 // it is not the last object to be send					 rsp.setInvokeId(new InvokeId(id+1));					 rsp.setLinkedId(ind.getInvokeId());				     }				 else				     {					 // it is the last object					 rsp.setInvokeId(ind.getInvokeId());					 rsp.setLinkedId(ind.getInvokeId());				     }			     }			 else			     {				 // only one object has been selected				 rsp.setInvokeId(ind.getInvokeId());			     };			 try			     {				 rsp.doIt();   	 System.out.println("On envoie une reponse");			     }			 catch (Exception e)			     {System.out.println("An exception occured during response issuance! " + e);			     }		     };		 for (Enumeration enum = v.elements(); enum.hasMoreElements();)		     { try			 {			     mib.remove((DistinguishedName) enum.nextElement());			 }		     catch (Exception excep)			 {			     System.out.println("Remove Exception" + excep);			 }		     };	     }	 catch (NoSuchObjectInstanceException e)	     {System.out.println("The Object Instance does not exists!");}	 catch (InvalidScopeException g)	     {System.out.println("Invalid Scope!");}	 catch (InvalidFilterException h)	     {System.out.println("Invalid Filter!");};	     }	 else	     { // issue an acces denied response	     }	     }}/** * The Sample Print Service Agent class * provides an instanciation of the MIB and stack listeners for the get operation */public class SamplePrintServiceAgent {    private static void runAgent() throws CMISException, java.io.IOException    {	// The Agent's name	String agentName= "CmisJavaApiSamplePrintServiceAgent";	// Building the managed objects	PrintService printService =  new PrintService(1);	Printer printer1 = new Printer(1,printService);	PrintQueue pq1 = new PrintQueue(1,3,true,true,"enabled",printService);	PrintJob pj1 = new PrintJob(153,"text", "foils", 1,pq1);	PrintJob pj2 = new PrintJob(154,"xml", "standard", 2,pq1);	PrintJob pj3 = new PrintJob(155,"gif", "hq", 3,pq1);	PrintJob pj4 = new PrintJob(156,"postscript", "standard", 2,pq1);	// Initializing the managed instance tree	ManagedObjectTree mib = new ManagedObjectTree();	try	    {		mib.add(((ObjectInstance) printService.getManagedObjectInstance()).getDistinguishedName(),printService);		mib.add(((ObjectInstance)printer1.getManagedObjectInstance()).getDistinguishedName(),printer1);		mib.add(((ObjectInstance)pq1.getManagedObjectInstance()).getDistinguishedName(),pq1);		mib.add(((ObjectInstance)pj1.getManagedObjectInstance()).getDistinguishedName(),pj1);		mib.add(((ObjectInstance)pj2.getManagedObjectInstance()).getDistinguishedName(),pj2);		mib.add(((ObjectInstance)pj3.getManagedObjectInstance()).getDistinguishedName(),pj3);		mib.add(((ObjectInstance)pj4.getManagedObjectInstance()).getDistinguishedName(),pj4);	    }	catch (Exception e)	    {		System.out.println("An error occured during MO insertion in the MIT : "+ e);	    };	// creating a CMIS Agent	AgentImpl agent =new AgentImpl(agentName);	// Adding listeners and error handlers	agent.addGetListener(new SamplePrintServiceGetListener(mib));	agent.addCreateListener(new SamplePrintServiceCreateListener(mib,pq1));	agent.addSetListener(new SamplePrintServiceSetListener(mib));	agent.addDeleteListener(new SamplePrintServiceDeleteListener(mib,pq1));	agent.addInternalErrorListener(new InternalErrorListenerBasicImpl());	agent.setSynchronousErrorHandler(new ErrorHandlerBasicImpl());    }    public static void main(String[] args) throws  CMISException, java.io.IOException    {	runAgent();    }} // SamplePrintServiceAgent

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -