保存在groovy中时无法使数据库状态与会话错误同步

时间:2012-01-27 16:01:18

标签: grails groovy

在我的groovy控制器的保存方法中尝试保存/创建包时,我收到以下错误和堆栈跟踪。

Error 500: Executing action [save] of controller [se.accumulate.wizard.SubmissionController] caused exception:
  could not deserialize; nested exception is
    org.hibernate.type.SerializationException: could not deserialize java.io.StreamCorruptedException
      at java.util.Hashtable.reconstitutionPut(Hashtable.java:889)
      at java.util.Hashtable.readObject(Hashtable.java:861)
      at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
      at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1846)
      at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
      at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
      at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
      at se.accumulate.wizard.PackagingService$$EnhancerByCGLIB$$2b5048f.createPackage(<generated>)

有关解决此问题的任何想法/提示。我已经检查了域与数据库等的任何不一致。一切看起来都很好。

示例代码:

def createPackage(Submission submission){
        logger.info("Creating submission package");
        def xml = createZip(submission);
    }

def createZip(Submission submission){
        def sw = new StringWriter()
        def xml = new groovy.xml.MarkupBuilder(sw);
        Customer customer = Customer.get(submission.customerId);
        String custDir = customer.ftpCustomerTempDirectory
        logger.info("Zip file: " + submission.fileName);

        final int BUFFER = 2048;
        try {
            String tmpFileName = custDir+"/" + java.util.UUID.randomUUID().toString().replaceAll("-", "") + ".zip";

            FileOutputStream fos = new FileOutputStream(tmpFileName);
            BufferedOutputStream dest;
            ZipOutputStream zos = new ZipOutputStream(fos);
            FileInputStream fis = new FileInputStream(custDir+"/" + submission.fileName);
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
            ZipEntry entry;
            while((entry = zis.getNextEntry()) != null) {
                //System.out.println("Extracting: " +entry);

                if (!entry.isDirectory()) {
                    if((entry.getName().toLowerCase().endsWith(".jar") || entry.getName().toLowerCase().endsWith(".jad") || entry.getName().toLowerCase().endsWith(".apk"))){
                        int count;
                        byte[] data = new byte[BUFFER];

                        String name = entry.getName();
                        if (name.indexOf('/') >= 0) {
                            String[] parts = name.split('/');
                            name = parts[parts.length - 1];
                        }
                        zos.putNextEntry(new ZipEntry("Applications/" + name));
                        while ((count = zis.read(data, 0, BUFFER))!= -1) {
                            zos.write(data, 0, count);
                        }
                        zos.closeEntry();
                    }
                }
            }
            zis.close();

            if (submission.applicationImage != null) {
                zos.putNextEntry(new ZipEntry("thumbnail_60x60.png"));
                zos.write(submission.applicationImage);
                zos.closeEntry();
            }

            zos.putNextEntry(new ZipEntry("Submission.xml"));
            zos.write(createXml(submission).toString().getBytes("UTF-8"));
            zos.close();


            FtpClientService fcs = new FtpClientService();
            fcs.putFile(customer.ftpUser, customer.ftpPassword, customer.ftpHost, customer.ftpPackagedDirectory, tmpFileName, submission.outputFileName);
            logger.info("check 1")
            if (!new File(tmpFileName).delete()) {
                logger.info("Could not delete tmp file " + tmpFileName);
            }
            logger.info("check 2")
            if (!new File(custDir+"/" + submission.fileName).delete()) {
                logger.info("Could not delete submission file " + submission.fileName);
            }
            logger.info("check 3")
        } catch(Exception e) {
            e.printStackTrace();
        }
        logger.info("check 4")
        return "sw";
    }

已显示所有日志检查1到4,并且已创建预期的输出/文件但是已抛出此异常...

我的域类:( Submission.groovy,哪个extneds向导)

package se.accumulate.wizard

class Submission extends Wizard {
    long operatingSystemId
    long deploymentId
    int newOrExisting

    String applicationName
    String applicationShortName
    String reportingName
    byte[] applicationImage
    long contentProviderId

    String fileName
    String outputFileName

    Properties deviceMapping

    long applicationId

    int overwriteExistingApplicationDetails

    static constraints = {
        operatingSystemId (nullable:true)
        deploymentId (nullable:true)
        newOrExisting (nullable:true)
        applicationName (nullable:true)
        applicationShortName (nullable:true)
        reportingName (nullable:true)
        applicationImage (nullable:true, maxSize: 1048576)
        contentProviderId (nullable:true)
        fileName (nullable:true)
        deviceMapping (nullable:true, maxSize: 5242880)
        outputFileName (nullable:true)
    }
}

package se.accumulate.wizard

abstract class Wizard {
    Date dateCreated
    Date lastUpdated
    long createdByUserId
    long customerId
    Boolean isConfirmed = false
    int pageTracker
}

我的数据库表提交说明如下:(抱歉格式不正确)

字段类型空密钥默认值

'application_id','bigint(20)','NO','',NULL,'' 'application_image','blob','YES','',NULL,'' 'application_name','varchar(255)','YES','',NULL,'' 'cms_id','varchar(255)','YES','',NULL,'' 'content_provider_id','bigint(20)','NO','',NULL,'' 'created_by_user_id','bigint(20)unsigned','NO','',NULL,'' 'date_created','datetime','NO','',NULL,'' 'deployment_id','bigint(20)','NO','',NULL,'' 'device_mapping','blob','YES','',NULL,'' 'file_name','varchar(255)','YES','',NULL,'' 'is_confirmed','tinyint(1)','NO','',NULL,'' 'last_updated','datetime','NO','',NULL,'' 'new_or_existing','int(11)','NO','',NULL,'' 'operating_system_id','bigint(20)','NO','',NULL,'' 'overwrite_existing_application_details','int(11)','NO','',NULL,'' 'customer_id','bigint(20)','YES','MUL',NULL,'' 'output_file_name','varchar(255)','YES','',NULL,'' 'reporting_name','varchar(255)','YES','',NULL,'' 'application_short_name','varchar(255)','YES','',NULL,'' 'page_tracker','int(11)','YES','',NULL,''

从createPackage调用的另一个方法的一部分(更改此方法后,错误显示) - 我刚刚过滤了“?”从文件名

def createXml(Submission submission){
    Application application = Application.get(submission.getApplicationId());
    ContentProvider contentProvider = ContentProvider.get(submission.getContentProviderId());

    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    Element rootElement = doc.createElement("game");
    rootElement.setAttribute("title", submission.applicationName);
    rootElement.setAttribute("short", submission.applicationShortName);
    rootElement.setAttribute("externalRef", application.cmsId);
    rootElement.setAttribute("contentPartner", contentProvider.name);

    if (submission.applicationImage != null) {
        Element image = doc.createElement("image");
        image.setAttribute("type", "thumbnail");
        image.setAttribute("mime-type", "image/png");
        image.setAttribute("width", "60");
        image.setAttribute("height", "60");
        image.setTextContent("thumbnail_60x60.png");
        rootElement.appendChild(image);
    }

    Properties ps = submission.deviceMapping;
    Properties dm = (Properties) ps;
    for (def device : dm) {
        device.key = device.key.split("\\?")[0];
        logger.debug("key=${device.key}, value=${device.value}")
        if (!(device.value == null || "null".equalsIgnoreCase(device.value))) {
            logger.info("Adding handset: " + device.value);
            Element handset = doc.createElement("handset");
            handset.setAttribute("name", device.value);

            String name = device.key;
            if (name.indexOf('/') >= 0) {
                String[] parts = name.split('/');
                name = parts[parts.length - 1];
            }

            if (name.toLowerCase().endsWith(".jad")) {
                // java and blackberry
                Element jadFile = doc.createElement("jadfile");
                jadFile.setTextContent("Applications/" + name);
                handset.appendChild(jadFile);

                /*
                name = getJarFileName();
                if (name.indexOf('/') >= 0) {
                    String[] parts = name.split('/');
                    name = parts[parts.length - 1];
                }*/

                Element jarFile = doc.createElement("jarfile");
                jarFile.setTextContent("Applications/" + name.replace(".jad", ".jar"));
                handset.appendChild(jarFile);
            }
            else {
                // android
                Element jarFile = doc.createElement("jarfile");
                jarFile.setTextContent("Applications/" + name);
                handset.appendChild(jarFile);
            }

            rootElement.appendChild(handset);
        }
    }
    doc.appendChild(rootElement);

    StringWriter xmlString = new StringWriter();
    try{
        Result result = new StreamResult(xmlString);
        Source source = new DOMSource(doc);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);

    } catch (Exception e){
        e.printStackTrace();
    }
    logger.info("XML: " + xmlString.toString());

    return  xmlString;
}

1 个答案:

答案 0 :(得分:0)

通过

  

在createXml方法中将属性更改为String,错误已被消除....

Properties ps = submission.deviceMapping;
    Properties dm = (Properties) ps;
    for (def device : dm) {
        device.key = device.key.split("\\?")[0];

替换为

java.util.Properties dm = submission.deviceMapping;
        for (String fileName : dm.keys()) {
            String device = dm.getProperty(fileName);
            fileName = fileName.split("\\?")[0];