Skip to main content
summaryrefslogtreecommitdiffstats
blob: c9d4f0fc3fa1843fd566b259190a7ba05684636d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Martin Taal - initial API and implementation
 *    Eike Stepper - maintenance
 */
package org.eclipse.emf.cdo.server.internal.hibernate;

import org.eclipse.emf.cdo.server.internal.hibernate.bundle.OM;

import org.eclipse.net4j.util.WrappedException;
import org.eclipse.net4j.util.io.IOUtil;
import org.eclipse.net4j.util.om.trace.ContextTracer;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Reads the hibernate mapping file from one or more resource locations and adds them to the configuration.
 * 
 * @author Martin Taal
 */
public class FileHibernateMappingProvider extends HibernateMappingProvider
{
  private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, FileHibernateMappingProvider.class);

  private final String mappingFileLocation;

  public FileHibernateMappingProvider(String mappingFileLocation)
  {
    if (mappingFileLocation == null || mappingFileLocation.length() == 0)
    {
      throw new IllegalArgumentException("mappingFileLocation"); //$NON-NLS-1$
    }

    this.mappingFileLocation = mappingFileLocation;
  }

  public String getMapping()
  {
    if (TRACER.isEnabled())
    {
      TRACER.trace("Adding hibernate mapping from location(s): " + mappingFileLocation); //$NON-NLS-1$
    }

    InputStream is = null;

    try
    {
      is = getClass().getResourceAsStream(mappingFileLocation);

      StringBuilder sb = new StringBuilder();
      String line;
      BufferedReader reader = new BufferedReader(new InputStreamReader(is, CDOHibernateConstants.UTF8));
      while ((line = reader.readLine()) != null)
      {
        sb.append(line).append(CDOHibernateConstants.NL);
      }
      return sb.toString();
    }
    catch (Exception e)
    {
      throw WrappedException.wrap(e);
    }
    finally
    {
      IOUtil.close(is);
    }
  }
}

Back to the top